NSManagedObjectContext does not see changes (“hasChanges” always false)

依然范特西╮ 提交于 2020-01-03 02:56:12

问题


I have what I thought is a very simple setup using core data: one NSManagedObjectContext and two NSFetchedResultsControllers. The app has its own subclass of NSManagedObject.

What I do is more or less the following (pseudo code/flow):

1. create an NSFetchRequest
    -> with it an NSFetchedResultsController
       (and attach an NSFetchedResultsControllerDelegate)
2. fetch the objects from the store
3. create another NSFetchRequest (in/from/with the same context)
    -> with it a second NSFetchedResultsController
4. fetch a subset of objects
5. change an object via
    [[[secondFetchedResultsController fetchedObjects] objectAtIndex:0]
            setWhateverSyncedProperty:differentValue]
6. save the NSManagedObjectContext (-> no error)
7. restart app -> changes lost

What I think I read in the documentation is that a) the first FetchedResultsController's delegate should get an controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: event and b) the NSManagedObjectContext saved the changes to the persistent store. Nothing of this happens.

I would also expect the NSManagedObjectContext to "see" the changes just before the save, i.e. return YES when I call "hasChanges" on it. It does not. This probably also is the reason for not saving anything to the persistent store: it thinks there are no changes.

Strangely enough though, when I "NSLog" the Context's contents I see the changed object. So the change is in memory...

Last but not least: when calling

[[self managedObjectContext] refreshObject:[[[self fetchedResultsController] fetchedObjects] objectAtIndex:2] mergeChanges:YES];

the delegate gets the didChangeObject-message!

I have a feeling that this could be related to the "Key-Value relevant notes" under hasChanges in the NSManagedObjectContext Class Reference but I don't grok the concept, I guess.

Any ideas and/or hints are greatly appreciated.

By the way: I am in iPhone simulator and on XCode 3.2.5.


回答1:


Make sure you have a strong reference to your NSManagedObjectContext. It sounds like you're loosing the context and the managed objects lost their context. That would explain changes not getting saved.




回答2:


It happened to me before. make sure your properties are @dynamic and not @synthesize



来源:https://stackoverflow.com/questions/8826967/nsmanagedobjectcontext-does-not-see-changes-haschanges-always-false

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!