主线程上的 NSFetchedResultsController 忽略从不同线程保存到核心数据?

发布于 2024-12-22 07:51:52 字数 1948 浏览 2 评论 0原文

我在主线程上有一个 NSFetchedResultsController 。同样从主线程,我异步发送 JSON 网络请求。当该 JSON 字符串返回时,我启动一个 NSOperation 来初始化一个新的(后台)NSManagedObjectContext,解析 JSON 字符串,创建/更新 NSManagedObject' s,并将它们保存在上下文中。后台上下文与主上下文具有相同的 persistenceStore。有了这个,我有两个问题:

  1. 我认为从任何上下文(在任何线程上)到持久存储的任何保存都会通知主NSFetchedResultsController有更改,但到目前为止它还没有不接受任何更改。我应该做些什么来通知主线程的 NSFetchedResultsController 存在外部保存,以便 tableView 相应更新?

  2. 因此,在主线程上,我订阅 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:

  1. 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's NSFetchedResultsController that there were external save's so that the tableView updates accordingly?

  2. So, on the main thread, I subscribe to NSManagedObjectContextWillSaveNotification and correctly see when all contexts (including those existing entirely on a separate thread) perform a save operation. The apple docs say that the notification.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, the userInfo is always nil 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 技术交流群。

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

发布评论

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

评论(1

海的爱人是光 2024-12-29 07:51:52

您观察到错误的通知:您需要观察的通知的名称是 NSManagedObjectContextDidSaveNotification(而不是 NSManagedObjectContextWillSaveNotification)。

You observe the wrong notification: the name of the notification you need to observe is NSManagedObjectContextDidSaveNotification (not NSManagedObjectContextWillSaveNotification).

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