2011-08-10 102 views
2

我正在使用UITableView为iPhone实现简单的Twitter客户端。我取我进每个用户的Twitter的图片时,其细胞中出现的tableView:的cellForRowAtIndexPath:在完成后台线程后更新UI

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    UIImage *profileImage = [tweet.user getProfileImageDataInContext:self.fetchedResultsController.managedObjectContext]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     cell.imageView.image = profileImage; 
    }); 
}); 

这里来获取图像的代码:

if (!self.profileImage) 
{ 
    sleep(2); 
    self.profileImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.profileImageURL]]; 
    //// if we recently scheduled an autosave, cancel it 
    [TwitterUser cancelPreviousPerformRequestsWithTarget:self selector:@selector(autosave:) object:context]; 
    // request a new autosave in a few tenths of a second 
    [TwitterUser performSelector:@selector(autosave:) withObject:context afterDelay:0.2]; 
} 
return [UIImage imageWithData:self.profileImage]; 

这里是我的错误得到:

twitterClient[10743:15803] bool _WebTryThreadLock(bool), 0x59bac90: 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... 

我认为还值得一提的是,这种情况发生时,当我尚未填充尚未填充表快速浏览。

我想在完成下载后更新主UI。实际的iPhone的twitter应用程序做得很好。

有什么建议吗?

+1

您能告诉我们该通话崩溃吗?跳到主线程(通过主队列,如果您正在使用GCD)来推送图像应该是正确的。 – Tommy

+0

谢谢,汤米。我用我的实际电话和崩溃编辑了这个问题。 – toddbranch

回答

4

什么是崩溃?这样的事情一个漂亮的标准模式是

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    // do background stuff 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     // do main-queue stuff, like updating the UI 
    }); 
}); 
+0

谢谢,诺亚。我用我的实际执行和崩溃编辑了上面的问题。 – toddbranch