2011-09-21 50 views
1

我基本上試圖重新加載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裏面: enter image description here

這是爲什麼?如何解決這個問題?

回答

4

錯誤是告訴你一定的方法必須在主線程(或者你沒有訪問的web線程)上運行。由於reloadData觸發這個方法最終要解決這個辦法之一是使reloadData執行主線程:

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

所以只是把該塊裏面? – aherlambang

+0

是的,用我提出的代碼替換你的代碼行[self.tableView reloadData];'。 – DarkDust

+0

另外,您需要使用鎖來防止對'self.notes'的併發訪問(除非它已經是線程安全的容器)。 – DarkDust