Firstly, thank you for the wonderful iOS Apprentice Swift tutorials.
I’ve worked my way through Checklists and MyLocations (that was a bit trickier) and I had, after Checklists, created a simple list app (with some smarts) with an Add/Edit page using the plist data model and a ‘datamodel.swift’ file… but it’s dawned on me that using Core Data would be a much better way to go about storing the data.
Question is: how on earth do I do this cleanly? I don’t need anything fancy - just want to swap out all the ‘plist’ related stuff for ‘core data’ stuff.
I’ve tried by using elements of the MyLocations tutorial but I end up getting hopeless muddled & lost, mainly because it really doesn’t translate to the ‘Checklists’ model very well. I get some functionality but it’s ultimately not working.
It would be nice to see a tutorial which takes the completed ‘Checklists’ and then updates it to use Core Data instead: the steps involved; traps & pitfalls; migrating plist data to Core Data, etc.
Any chance of that or, if not, any chance you can point me in the right direction to be able to simple swap out my plist DataModel for a Core Data DataModel?
Number one step is to draw the data model. Half the work in Core Data is getting the relationships right - if you don’t have any (simple list of tasks) then this shouldn’t be hard. I would be looking at the working datamodel.swift and treating that as an abstraction. What you need to do is leave the rest of the app alone and to write the Core Data stuff to replace the plist stuff…
- when you want to get a saved object (or to see one exists before you create it) you want a fetch request with a predicate that identifies the item. This is analogous to getting the dictionary and using the key to get the object you want.
- for table views, look into NSFetchedResultsController. See this tutorial for details - it’s an older one, but still useful.
- to persist your data you need to use
save on the NSManagedObjectContext - analogous to writing the plist to the file store
- passing the NSManagedObjectContext around has been the subject of much debate. Some use a reference to the app delegate and grab it from anywhere in the app but it is considered bad practise. If using the fetched results controller you will need the managed object context in every view controller with a table view, so get into the habit of passing a reference to the managed object context in prepareForSegue or similar.
Wonderful, thank you.
I managed to work it out for myself and my solution was pretty much what you have suggested. The key was to not modify too much in the various view controllers and let the Core Data data model do the work. I’m passing the NSManagedObjectContext around using prepareForSegue and it works well.
I even managed to solve a bug which only appeared on the iPad. When it came to picking an image from the Photo Library, the iPad crashed. Turns out I needed to use a Popover controller - and I managed to get that working too.
I submitted the app yesterday and it’s already been approved! Thanks again for the help and the wonderful tutorials. 5 weeks ago I had never, ever written a computer program. I now have two apps for sale.
That’s amazing - congratulations! It’s a great feeling to help out.
What’s the name of your app? I’d like to check it out.
It’s a fairly specialised target audience so you probably won’t want to buy it! Here it is:
And my very first app, which I made before finding this great site:
Also a pretty narrow target audience! I need to find my Flappy Bird…
Thanks, I’m interested in what others are able to produce at the start of their iOS journeys
You have actually posted and asked your question and issue in the wrong thread, forum and category after all for sure and for good indeed: this is an iOS Apprentice related query and you are in the Swift Apprentice forum. Please post your question in the appropriate, corresponding, associated and correct thread over here instead: https://forums.raywenderlich.com/c/books/ios-apprentice in order to get a proper answer, reply and solution to it after all for sure and for good indeed. Thank you! :]