pbd*_*pbd 1 uitableview grand-central-dispatch ios objective-c-blocks
在预取数据和在uitableview上显示时遇到问题.所以基本上我想阻止主UI线程,以便我可以从Web获取数据.我正在使用串行调度队列进行同步.此外,调度队列块正在执行从Web获取数据的另一个块.执行代码是用viewdidload编写的:
dispatch_queue_t queue= dispatch_queue_create("myQueue", NULL);
CMStore *store = [CMStore defaultStore];
// Begin to fetch all of the items
dispatch_async(queue, ^{
[store allObjectsOfClass:[Inventory class]
additionalOptions:nil
callback:^(CMObjectFetchResponse *response) {
//block execution to fetch data
}];
});
dispatch_async(queue, ^{
//load data on local data structure
[self.tableView reloadData];
});
Run Code Online (Sandbox Code Playgroud)
除了主线程/队列之外,您不应该在任何地方执行任何与UI相关的代码.
始终在主线程/队列上执行每个与UI相关的代码(如reloadDataon UITableView).在您的示例中,我猜您还应该仅在获取数据时重新加载tableview,因此在完成块中,而不是在调用回调之前.
// Begin to fetch all of the items
dispatch_async(queue, ^{
[store allObjectsOfClass:[Inventory class]
additionalOptions:nil
callback:^(CMObjectFetchResponse *response) {
// block execution to fetch data
...
// load data on local data structure
...
// Ask the main queue to reload the tableView
dispatch_async(dispatch_get_main_queue(), ^{
// Alsways perform such code on the main queue/thread
[self.tableView reloadData];
});
}];
});
Run Code Online (Sandbox Code Playgroud)