NSManagedObjectContext没有看到更改(“hasChanges”始终为false) [英] NSManagedObjectContext does not see changes ("hasChanges" always false)
问题描述
我有我认为是一个非常简单的设置使用核心数据:一个 NSManagedObjectContext
和两个 NSFetchedResultsController
。该应用程序有自己的子类 NSManagedObject
。
我所做的是或多或少以下(伪代码/流):
1。创建NSFetchRequest
- >使用它NSFetchedResultsController
(和附加一个NSFetchedResultsControllerDelegate)
2.从商店获取对象
3.创建另一个NSFetchRequest(在/ from /具有相同的上下文)
- >使用它第二个NSFetchedResultsController
4.获取对象的子集
5.通过
更改一个对象[[[secondFetchedResultsController fetchedObjects] objectAtIndex:0]
setWhateverSyncedProperty:differentValue]
6.保存NSManagedObjectContext( - >无错误)
7.重启app - >更改丢失
我认为我在文档中读到的是a)第一个FetchedResultsController的委托应该得到一个 controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
事件和b)NSManagedObjectContext将更改保存到持久存储。这不会发生。
我也希望 NSManagedObjectContext
可以看到即当我调用hasChanges时返回 YES
。它不是。这可能也是不保存任何东西到永久存储的原因:它认为没有更改。
奇怪的是,当我NSLog的上下文的内容我请参阅已更改对象。因此变化在内存中...
最后但并非最不重要:当调用
[[self managedObjectContext] refreshObject:[[[self fetchedResultsController] fetchedObjects] objectAtIndex:2] mergeChanges:YES];
委托获取 didChangeObject
-message!
顺便说一下,我的想法和/或提示非常感谢。我在iPhone模拟器和XCode 3.2.5。
请确保您的属性 @dynamic
,而不是 @synthesize
I have what I thought is a very simple setup using core data: one NSManagedObjectContext
and two NSFetchedResultsController
s. 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.
It happened to me before. make sure your properties are @dynamic
and not @synthesize
这篇关于NSManagedObjectContext没有看到更改(“hasChanges”始终为false)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!