使用块来重新加载UITableView数据崩溃
问题描述:
我基本上试图重新加载UITableView数据时使用块数据以非阻塞的方式加载回来。使用块来重新加载UITableView数据崩溃
[query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError * error){
if (!error){
for (PFObject * vote in objects){
if([vote objectForKey:@"note"]){
NSString * username = ((PFUser *)[vote objectForKey:@"voter"]).username;
NSLog(@"Username is %@", username);
[self.notes addObject:[NSDictionary dictionaryWithObject:[vote objectForKey:@"note"] forKey:username]];
NSLog(@"Note is %@", [vote objectForKey:@"note"]);
}
}
[self.tableView reloadData];
}
//[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
}];
的问题是,下面的代码将产生一个错误:
bool _WebTryThreadLock(bool), 0x246d20: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
这是我的cellForRowAtIndexPath里面:
这是为什么?如何解决这个问题?
答
错误是告诉你一定的方法必须在主线程(或者你没有访问的web线程)上运行。由于reloadData
触发这个方法最终要解决这个办法之一是使reloadData
执行主线程:
[self.tableView
performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO
];
所以只是把该块里面? – aherlambang
是的,用我提出的代码替换你的代码行[self.tableView reloadData];'。 – DarkDust
另外,您需要使用锁来防止对'self.notes'的并发访问(除非它已经是线程安全的容器)。 – DarkDust