主线程上的 NSFetchedResultsController 忽略从不同线程保存到核心数据?
我在主线程上有一个 NSFetchedResultsController
。同样从主线程,我异步发送 JSON 网络请求。当该 JSON 字符串返回时,我启动一个 NSOperation
来初始化一个新的(后台)NSManagedObjectContext
,解析 JSON 字符串,创建/更新 NSManagedObject
' s,并将它们保存在上下文中。后台上下文与主上下文具有相同的 persistenceStore。有了这个,我有两个问题:
我认为从任何上下文(在任何线程上)到持久存储的任何保存都会通知主
NSFetchedResultsController
有更改,但到目前为止它还没有不接受任何更改。我应该做些什么来通知主线程的 NSFetchedResultsController 存在外部保存,以便tableView
相应更新?因此,在主线程上,我订阅
NSManagedObjectContextWillSaveNotification
并正确查看所有上下文(包括完全存在于单独线程上的上下文)何时执行save
操作。 苹果文档说notification.userInfo
应该有一个包含 3 个数组的字典,一个数组对应后台线程上的每个“更新、删除和插入”模型对象。但是,userInfo
对我来说始终是nil
。知道我做错了什么吗?
订阅 AppDelegate 中的 NSManagedObjectContextWillSaveNotification:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(managedObjectContextDidSave:)
name:NSManagedObjectContextWillSaveNotification
object:nil];
以及 AppDelegate 中保存上下文的方法:
- (void)managedObjectContextDidSave:(NSNotification *)notification {
DLog(@"notification: %@", notification); //not nil
DLog(@"notification user info: %@", notification.userInfo); // always nil... why??
NSManagedObjectContext *theContext = notification.object;
if(theContext != context) {
DLog(@"---- SAVED ON ANOTHER CONTEXT");
// should I notify NSFetchedResultsController that there were context saves on background threads?
// how can I merge contexts if userInfo is nil?
}
}
我还想知道处理多线程(使用单独的 NSManagedObjectContexts)和核心数据的最佳实践。
I have an NSFetchedResultsController
on the main thread. Also from the main thread, I asynchronously send out a network request for JSON. When that JSON string returns, I start an NSOperation
that inits a new (background) NSManagedObjectContext
, parses the JSON string, creates/updates NSManagedObject
's, and saves them in the context. The background context has the same persistentStore as the main context. With this, I have 2 questions:
I thought that any saves to the persistent store from any context (on any thread) would notify the main
NSFetchedResultsController
that there are changes, but so far it doesn't pick up any changes. Is there something I should do to notify the main thread'sNSFetchedResultsController
that there were externalsave
's so that thetableView
updates accordingly?So, on the main thread, I subscribe to
NSManagedObjectContextWillSaveNotification
and correctly see when all contexts (including those existing entirely on a separate thread) perform asave
operation. The apple docs say that thenotification.userInfo
should have a dictionary of 3 arrays, one array for each of the "updated, deleted, and inserted" model objects on the background thread. However, theuserInfo
is alwaysnil
for me. Any ideas what I'm doing wrong?
Subscribing to the NSManagedObjectContextWillSaveNotification
in AppDelegate:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(managedObjectContextDidSave:)
name:NSManagedObjectContextWillSaveNotification
object:nil];
And the method for when contexts are saved in AppDelegate:
- (void)managedObjectContextDidSave:(NSNotification *)notification {
DLog(@"notification: %@", notification); //not nil
DLog(@"notification user info: %@", notification.userInfo); // always nil... why??
NSManagedObjectContext *theContext = notification.object;
if(theContext != context) {
DLog(@"---- SAVED ON ANOTHER CONTEXT");
// should I notify NSFetchedResultsController that there were context saves on background threads?
// how can I merge contexts if userInfo is nil?
}
}
I'd also like to know the best practices in dealing with multiple threads (with separate NSManagedObjectContexts) and Core Data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您观察到错误的通知:您需要观察的通知的名称是 NSManagedObjectContextDidSaveNotification(而不是 NSManagedObjectContextWillSaveNotification)。
You observe the wrong notification: the name of the notification you need to observe is
NSManagedObjectContextDidSaveNotification
(notNSManagedObjectContextWillSaveNotification
).