没有在dispatch_async中调用UITableView endUpdates [英] UITableView endUpdates not being called in dispatch_async
问题描述
我有几个表视图,这些表视图将 JSON 请求发送到服务器,将结果存储在核心数据中,并使用NSFetchedResultsController
显示它们.我正在对GCD进行如下实验:
I have several table views that send JSON requests to a server, store the results in core data, and display them using an NSFetchedResultsController
. I was experimenting with GCD as follows:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
// Perform JSON requests.
dispatch_async(dispatch_get_main_queue(), ^{
[theTableView reloadData];
});
});
但是,这会导致UI中发生一些奇怪的事情.新的托管对象将呈现空白单元格,删除的托管对象将导致单元格重叠等.
However, this would cause some weird things to happen in the UI. New managed objects would render blank cells, deleted managed objects would cause cells to overlap, etc.
但是,我发现如果执行此操作,一切都将正确呈现.
However, I found that if I did this, everything would render correctly.:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
dispatch_async(dispatch_get_main_queue(), ^{
[theTableView endUpdates];
});
}
我想知道的是,为什么这是必要的?由于它是[theTableView reloadData]
的结果而触发,为什么它不自动包含在主队列中?我以为也许是因为我没有明确地称呼它.在那种情况下,我是否必须同样包装所有功能?
What I wanted to know is, why is this necessary? Since it fires as a result of [theTableView reloadData]
, why isn't it automatically included in the main queue? I thought maybe that it was because I didn't call it explicitly. In that case, do I have to wrap all my functions similarly?
推荐答案
我假定,您使用的是单独线程中的主NSManagedObjectContext
,
不允许.对于后台导入,您必须创建一个单独的托管对象上下文,
导入该上下文中的对象,然后将更改保存/合并到主上下文中.
I assume that you use the main NSManagedObjectContext
from a separate thread, which
is not allowed. For a background import, you have to create a separate managed object context,
import the objects in that context, and then save/merge the changes to the main context.
一种可能性是使用主要管理对象上下文的子上下文:
One possibility is to use a child context of the main managed object context:
NSManagedObjectContext *childContext = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
childContext.parentContext = mainManagedObjectContext;
[childContext performBlock:^{
// Perform JSON requests.
// Save objects to childContext.
NSError *error;
BOOL success = [childContext save:&error];
}];
私有并发类型"的上下文创建其自己的队列/线程,以便执行performBlock
在后台(而且您不必/不必自己创建队列).
A context of the "private concurrency type" creates its own queue/thread, so that the performBlock
is executed
in the background (and you need/must not create a queue yourself).
保存childContext
会将更改合并到父级mainManagedObjectContext
.
对于获取的结果控制器来说,这应该足够了
更改,并更新表格视图.
Saving childContext
merges the changes up to the parent mainManagedObjectContext
.
This should be sufficent for the fetched results controller to get notified of the
changes, and update the table view.
请参阅 OS X v10.7和iOS 5.0的核心数据发行说明 有关托管对象上下文并发的更多信息, 和嵌套上下文.
See Core Data Release Notes for OS X v10.7 and iOS 5.0 for more information about managed object context concurrency, and nested contexts.
这篇关于没有在dispatch_async中调用UITableView endUpdates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!