如何同步两个 NSManagedObjectContext

发布于 2024-09-08 20:51:50 字数 1069 浏览 1 评论 0原文


我正在开发一个使用 coredata 的 ipad 应用程序。它下载网络数据库上的信息,并将其记录在 coredata 中。该应用程序基于分割视图。我的问题是在后台下载和记录数据。
这是我的做法:
- 我创建了一个 NSOperation,用于下载和记录数据。
- 此 NSOperation 使用与 appDelegate 上下文不同的 NSManagedObjectContext,由该函数返回,即在 appDelegate 中:

(NSManagedObjectContext*)newContextToMainStore {
     NSPersistentStoreCoordinator *coord = nil;
     coord = [self persistentStoreCoordinator];
     NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
     [moc setPersistentStoreCoordinator:coord]; 
     return [moc autorelease];
}

- 我在 NSOperation 中有一个观察者,当我保存上下文时,它将在 appDelegate 中调用此函数,也修改委托的上下文:

- (void)mergeChangesFromContextSaveNotification:(NSNotification*)notification {
     [[self managedObjectContext]mergeChangesFromContextDidSaveNotification:notification];
}

但我有一个问题,同步不起作用,因为 rootViewController (即 UITableViewController )上的数据具有使用 appDelegate 的上下文初始化的 NSManagedObjectContext 并用作数据源是 NSFetchedResultsController,不会自动实现信息,因为它通常必须这样做。
所以我问你:
我做错了什么?这是使用两个不同上下文并使它们同步的好方法吗?

I'm working on an ipad application that use coredata. It download information on a database that is on the web, and record them in coredata. The application is based on a split view. My problem was to make the download and the record of the data in background.
Here is how I've done :
- I've create an NSOperation, that does the download and the record of the data.
- This NSOperation use a different NSManagedObjectContext than the context of the appDelegate, return by this function, that is in the appDelegate :

(NSManagedObjectContext*)newContextToMainStore {
     NSPersistentStoreCoordinator *coord = nil;
     coord = [self persistentStoreCoordinator];
     NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init]; 
     [moc setPersistentStoreCoordinator:coord]; 
     return [moc autorelease];
}

- I've had an observer in the NSOperation, that will call this function in the appDelegate when I save the context, to modify the context of the delegate too :

- (void)mergeChangesFromContextSaveNotification:(NSNotification*)notification {
     [[self managedObjectContext]mergeChangesFromContextDidSaveNotification:notification];
}

But I've a problem, the synchronisation doesn't work, because the data on the rootViewController (that is a UITableViewController), that have a NSManagedObjectContext initialised with the context of the appDelegate and use as datasource a NSFetchedResultsController, don't actualise automatically the informations, as it normaly must do.
So I ask you :
What did I do wrong ? Is it the good way to use two different context and synchonise them ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愛上了 2024-09-15 20:51:50

你这里的内容看起来是正确的。您确实需要确保在 rootViewController 中实现 NSFetchedResultControllerDelegate 方法,以便更改将显示在 UI 中。

What you have here looks correct. You do want to make sure you implement the NSFetchedResultControllerDelegate methods in the rootViewController so the changes will appear in the UI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文