使用块重新加载UITableView数据崩溃

时间:2011-09-21 06:40:12

标签: iphone objective-c ipad

我基本上尝试在以非阻塞方式加载数据时使用块重新加载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中: enter image description here

为什么会这样,我该如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

错误告诉您必须在主线程(或您无权访问的Web线程)上运行某个方法。由于reloadData最终会触发此方法,因此解决此问题的一种方法是在主线程上执行reloadData

[self.tableView
    performSelectorOnMainThread:@selector(reloadData)
    withObject:nil
    waitUntilDone:NO
];