Should I be fetching my Core Data more often?
So right now when my app starts I get all of my data from Core Data. My data has entities of Groups and People. The Groups contain People. So as I go through the app I'm adding and deleting People and Groups.
My problem lies when I select a Group from a tableview and I pass that Group onto the next tableview. From that tableview I can press a button/row and add a bunch of People from a modal view. When I press Done from that modal view I need it to update the previous view with those new People, which it's not doing correctly.
It seems a little, for lack of a better word, wrong that I'm passing a lot of stuff around. Would it be better for me to use another fetch after I update People in a Group? From r开发者_如何学Pythoneading what other people say, then I could have different views listen for an updated Core Data and change their views accordingly.
I really hope this makes sense. I've been reading a lot to try to figure out the best approach but I'm not getting anything definite. I feel like my code is turning into spaghetti so I stopped and I'm trying to rethink it all. I'm also thinking maybe I need my very own Model class as opposed to just the classes that Core Data auto-generates.
From what you've written what you're doing sounds about right. I assume you're using a NSFetchedResultsController. Have you implemented the delegate methods outlined in the docs? Specifically, the controllerWillChangeContent
: method should give you what you're after.
Regarding implementing your own NSManagedObject subclass, take a look at MOGenerator.
I would recommend NSFetchedResultsController to you. See documentation at ...
http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html
Look at CoreDataBooks example, especially at RootViewController.m at ...
http://developer.apple.com/library/ios/#samplecode/CoreDataBooks/Listings/Classes_RootViewController_m.html%23//apple_ref/doc/uid/DTS40008405-Classes_RootViewController_m-DontLinkElementID_14
NSFetchedResultsControllerDelegate can help you to update your previous view easily. As I wrote, look at sample code to see how it really works.
精彩评论