【发布时间】:2012-05-25 20:18:58
【问题描述】:
大家好,当我尝试添加到空表视图时出现此错误:将第 0 行插入第 0 部分,但更新后第 0 部分中只有 0 行。有什么想法吗?
dispatch_queue_t fetchQ = dispatch_queue_create("Blog Fetcher", NULL);
dispatch_async(fetchQ, ^{
//pull this call out to Blog+twitter later saving should only take
//place in the model!
[document.managedObjectContext performBlock:^
{
NSArray* resultsArray = [self.tweets objectForKey:@"results"];
for (NSDictionary* internalDict in resultsArray)
{
User *user = [NSEntityDescription
insertNewObjectForEntityForName:@"User"inManagedObjectContext:self.context];
user.username =[internalDict objectForKey:@"from_user_name"];
[self.context save:nil];
self.list = [self.list arrayByAddingObject:user];
NSLog(@" indexpath set here %i",self.list.count-1);
NSIndexPath *newIndexpath =[NSIndexPath indexPathForRow:self.list.count-1 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}];
});
dispatch_release(fetchQ);
}
【问题讨论】:
-
正如@JefferyThomas 所暗示的,对 UIKit 对象(例如 tableview)的所有更新都必须从主线程完成。另外,请确保您的所有核心数据保存都在同一个线程上完成(不要在主线程和这个线程上进行!)。
标签: objective-c uitableview ios5