问题描述
我有一个主要的NSManagedObjectContext,它是在appDelegate中创建的。
I have a main NSManagedObjectContext that it's created in the appDelegate.
知道,我使用另一个NSManagedObjectContext来编辑/添加新对象而不影响主NSManagedObjectContext,直到我保存它们。
Know, I'm using another NSManagedObjectContext for editing/adding new objects without affecting the main NSManagedObjectContext, until I save them.
当我保存第二个NSManagedObjectContext时,更改不会反映在主NSManagedObjectContext中,但是如果我从模拟器打开.sqlite数据库,已正确保存到.sqlite数据库中。不管我是否再次获取数据,即使我创建了第三个NSManagedObjectContext,我看不到从第二个NSManagedObjectContext的更改,但这些更改在磁盘上这一刻...
When I save the second NSManagedObjectContext, the changes are not reflected in the main NSManagedObjectContext, but If I open the .sqlite database from simulator, the changes have been saved correctly into the .sqlite database. No matter if I fetch again the data, even if I create a third NSManagedObjectContext, I can't see the changes from the second NSManagedObjectContext, but those changes are on disk at this moment...
如果我退出并打开应用程序,所有更改都在那里。
If I quit and open the app, all changes are there.
什么可能导致主NSManagedObjectContext不能看到商店的新更改?
What can cause the main NSManagedObjectContext not to see the new changes of the store ?
在这种方法之前,我使用了相同的NSManagedObjectContext和undoManager,但我想更改它使用两个不同的NSManagedObjectContext。
Before this approach, I was using the same NSManagedObjectContext and undoManager, but I want to change it to use two different NSManagedObjectContext.
感谢,
m。
second NSManagedObjectContext save:
-----------------------------------------
NSError* error = nil;
if ([managedObjectContext hasChanges]) {
NSLog(@"This new object has changes");
}
if (![managedObjectContext save:&error]) {
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else {
NSLog(@" %@", [error userInfo]);
}
}
推荐答案
您尚未这样做,建议您阅读关于
If you haven't already done so, I suggest reading the Apple documentation on Core Data : Change Management.
您需要通知第一个上下文中通过第二个上下文保存的更改。当保存上下文时,它发布 NSManagedObjectContextDidSaveNotification
。注册该通知。在处理程序方法中,合并到第一上下文中通过第二上下文保存的更改。例如:
You need to notify the first context of the changes that were saved through the second context. When saving a context, it posts a NSManagedObjectContextDidSaveNotification
. Register for that notification. In the handler method, merge into the first context the changes saved through the second context. For example:
// second managed object context save
// register for the notification
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(handleDidSaveNotification:)
name:NSManagedObjectContextDidSaveNotification
object:secondManagedObjectContext];
// rest of the code ommitted for clarity
if (![secondManagedObjectContext save:&error]) {
// ...
}
// unregister from notification
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:NSManagedObjectContextDidSaveNotification
object:secondManagedObjectContext];
通知处理程序:
- (void)handleDidSaveNotification:(NSNotification *)note {
[firstManagedObjectContext mergeChangesFromContextDidSaveNotification:note];
}
这篇关于从一个NSManagedObjectContext保存的更改不反映在主NSManagedObjectContext上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!