问题
Could somebody point me in the correct direction for a full implementation example of NSManagedObjectContextObjectsDidChangeNotification
NSManagedObjectContextDidSaveNotification
and
NSManagedObjectContextWillSaveNotification
I have read the references to it here: http://developer.apple.com/library/ios/#documentation/cocoa/Reference/CoreDataFramework/Classes/NSManagedObjectContext_Class/NSManagedObjectContext.html
but I don't see a full example code of exactly where I should implement it so that my Views update correctly when I save my context.
In a current project I am saving and deleting in my NSManagedObjectContext
but my app is crashing for some save operations. From what I can tell I need to update my views so they implement NSManagedObjectContextSaveNotification
but I don't know where to add it into my code.
Is there an example project I could download somewhere?
回答1:
Magical Record would be a reference example: on github. But, rather than look at it and try to make your code work, just use MR to manage your Core Date setup instead.
A better option than using NSManagedObjectContextSaveNotification
is probably to use NSFetchedResultsController
. You're unlikely to need all added/removed objects when it happens, really you just want a trigger to refresh the UI and the FRC will do that for you. It'll also deal with getting the data you need as you need it (paged - if you configure it to do so) and appropriately sorted.
回答2:
I found this website to be very informative. Concurrency in core data is not a light subject, but it solves difficult problems that multi-threading creates.
来源:https://stackoverflow.com/questions/16700603/full-example-of-implementing-notifications-for-core-data-nsmanagedobject