Iphone 使用块重新加载UITableView数据崩溃
我基本上是在以非阻塞方式加载UITableView数据时尝试使用块重新加载该数据Iphone 使用块重新加载UITableView数据崩溃,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我基本上是在以非阻塞方式加载UITableView数据时尝试使用块重新加载该数据 [query findObjectsInBackgroundWithBlock:^(NSArray * objects, NSError * error){ if (!error){ for (PFObject * vote in objects){ if([vote objectForKey:@"note"]){
[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...
这是在我的CellForRowatineXpath中:
这是为什么?我如何修复它?错误告诉您必须在主线程(或您无权访问的web线程)上运行某个方法。由于
reloadData
最终会触发此方法,解决此问题的一种方法是在主线程上执行reloadData
:
[self.tableView
performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO
];
错误告诉您必须在主线程(或您无权访问的web线程)上运行某个方法。由于
reloadData
最终会触发此方法,解决此问题的一种方法是在主线程上执行reloadData
:
[self.tableView
performSelectorOnMainThread:@selector(reloadData)
withObject:nil
waitUntilDone:NO
];
那么就把它放在块内?是的,替换你的行
[self.tableView reloadData]
与我建议的代码。此外,您需要使用锁来防止并发访问self.notes
(除非它已经是线程安全容器)。我有一个MBProgressHUD,它在执行此操作时正在运行,应该足够了?我看不到这里的关系,您的MBProgressHUD似乎与您的表视图没有任何关系。是否用performselectornmainthread:
调用替换了reloadData
中的行?它修复了崩溃吗?那么就把它放在块中吧?是的,替换你的行[self.tableView reloadData]
与我建议的代码。此外,您需要使用锁来防止并发访问self.notes
(除非它已经是线程安全容器)。我有一个MBProgressHUD,它在执行此操作时正在运行,应该足够了?我看不到这里的关系,您的MBProgressHUD似乎与您的表视图没有任何关系。是否用performselectornmainthread:
调用替换了reloadData
中的行?它能解决撞车问题吗?