核心数据多线程 [英] Core Data multi-threading

查看:97
本文介绍了核心数据多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程式以展示其数据源是Core Data SQLite存储的tableview开头。当应用程序启动时,将创建具有自己的存储控制器和上下文的辅助线程,以从Web中获取存储中的数据的更新。但是,对存储的任何结果更改不会通知到fetchedresults控制器(我假设它有自己的协调器),因此该表不会随存储更改更新。什么是最有效的方式来刷新主线程上下文?我正在考虑跟踪在辅助线程上更改的任何对象的objectIDs,当辅助线程完成并调用[context refreshObject:...]时发送这些对主线程。任何帮助将非常感激。

解决方案

NSFetchedResultsController 中处理表,注册 viewDidLoad loadView

  [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave :) name:@ContextDidSaveobject:nil]; 



当辅助线程准备好新数据时,只需像往常一样保存上下文,然后发布通知:

  [[NSNotificationCenter defaultCenter] postNotificationName:@ContextDidSaveobject:managedObjectContext]; 

将使用以下方法在 NSFetchedResultsController 中处理:



EDIT :修改

   - (void)contextDidSave:(NSNotification *)下面的方法正确考虑多线程。 )notification 
{

SEL selector = @selector(mergeChangesFromContextDidSaveNotification :);
[[[[UIApplication sharedApplication] delegate] managedObjectContext] performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];

}

对于UI更新,可以使用 NSFetchedResultsController 委托方法。



最后,请记得在 NSFetchedResultsController dealloc $ c>以下内容:

  [[NSNotificationCenter defaultCenter] removeObserver:self]; 


My app starts by presenting a tableview whose datasource is a Core Data SQLite store. When the app starts, a secondary thread with its own store controller and context is created to obtain updates from the web for data in the store. However, any resulting changes to the store are not notified to the fetchedresults controller (I presume because it has its own coordinator) and consequently the table is not updated with store changes. What would be the most efficient way to refresh the context on the main thread? I am considering tracking the objectIDs of any objects changed on the secondary thread, sending those to the main thread when the secondary thread completes and invoking "[context refreshObject:....] Any help would be greatly appreciated.

解决方案

In your NSFetchedResultsController handling the table, register in viewDidLoad or loadView for a notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextDidSave:) name:@"ContextDidSave" object:nil];

When the secondary thread is ready with the new data, simply save the context as usual, and then post the notification:

[[NSNotificationCenter defaultCenter] postNotificationName:@"ContextDidSave" object:managedObjectContext];

The notification will be handled in your NSFetchedResultsController using the following method:

EDIT: modified the method below taking correctly into account multi-threading, after an insightful discussion with bbum.

- (void)contextDidSave:(NSNotification *)notification
{

    SEL selector = @selector(mergeChangesFromContextDidSaveNotification:);
    [[[[UIApplication sharedApplication] delegate] managedObjectContext] performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES];

}

For UI update, it can be done automatically using the NSFetchedResultsController delegate methods.

Finally, remember to add in the dealloc method of the NSFetchedResultsController the following:

[[NSNotificationCenter defaultCenter] removeObserver:self];

这篇关于核心数据多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆