I am trying to apply the concepts learned in the Checklists tutorial and I am having difficulty getting my data to persist. I did remember to hit the home button on the Simulator before pressing the Stop button in Xcode. The app is a simple TableView with a Drill-down, both embedded in NavControllers, and the Model is just a String and an Int, and I put an array of Models into the Store. I don’t get any errors, but the data won’t persist.
Here’s the GitHub URL: GitHub - ThomasHWhite/data-save: BetterModel project with data not persisting
If anyone out there is real familiar with how Checklists shows us how to save data, maybe you can see what I’ve done wrong?
I might be mistaken but I don’t see any data persistence code in your project. Where is the data actually being saved to a file?
Sorry MH, I don’t have a very good workflow. I posted the wrong version. Here’s the one with the data persistence (I hope):
I would check the following:
- does the file actually get created?
- does loadStore() actually do anything?
This should narrow it down a bit.
So I tried by responding to your suggestions by looking at the part of the tutorial that talks about creating the Documents directory, putting a print statement in the required init?() method to get the path, and then copying that in Finder to see the contents.
But when I hit the home button on the simulator, and then stop the simulator, no path gets printed. So, I guess that method never gets called. I’m having real trouble understanding what to do, because I’m trying to apply the saving and loading technique as shown at the end of the tutorial, and so referring to the earlier parts of the tutorial like the above mentioned part doesn’t help much, because by the time I get to the end of the tutorial, things have evolved quite a bit.
However, when I do hit the home button, and then stop the simulator, I am getting this message in the console:
“Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates.”
I have no idea what it means. Does it help to give me any more hints on where to look?
Oh, and I don’t know if this would affect anything, but this project was originally called BetterModel. I copied that folder and renamed it PersistingData, in order to work on the saving and loading component. The project is still called BetterModel, but does renaming the folder affect the path?:
return(documentsDirectory() as NSString).stringByAppendingPathComponent(“BetterModel.plist”)
Update: I put the print statement in the override init method instead, and I got a path. I checked the path after running the App on the simulator and entering a few items. These items did get saved in the documents folder, and the plist was called BetterModel.plist.
So I guess my problem lies in the loading? loadStore() is being called in the same override init() that has the print statement, so I know it’s getting executed (it comes right before the print statement), but I guess it’s not doing anything. Why it’s not doing anything is still a mystery. I’m thinking maybe it’s doing something, but I haven’t done anything to show the data on the initial view? Like maybe in the initial View Controller I need to somehow call loadStore() in viewDidLoad() ? --no, wait, that’s part of initializing it, so I just need to instantiate a store in viewDidLoad() ?
I’m letting you figure this out for yourself because this sort of debugging skill is invaluable.
First, make sure that saving works. You should see a BetterModel.plist file in the Documents folder for your app. When you open this file using Finder, it should not be empty.
If you’ve verified that saving works as it should, then that obviously isn’t the problem. In that case, the only thing it could be is the loading. So then you’d put some print() statements inside the loadStore() method to figure out exactly where it goes wrong.
If the loading also works correctly, then maybe the loaded data isn’t actually put into the Store or Model objects.
So the trick here is to use print() statements (or the debugger if you know how) to figure out step-by-step what is going on.
Thanks Matthijs. I actually had some time yesterday to give to this, so I went back to the Checklists tutorial, and I started from scratch. I applied all of your evolutions of the data model to my analogous app until I reached the end and got it working. Turns out, I wasn’t too far off. But in the process, I understood things a little better. But thanks for responding.