Я реализую простой твиттер-клиент для iPhone, используя UITableView. Я получаю изображение каждого пользователя Twitter в моем фиде, когда его ячейка появляется в tableView: cellForRowAtIndexPath:
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...
Я думаю, также стоит упомянуть, что это происходит, когда я очень быстро пролистываю представление таблицы, когда оно еще не заполнено.
Я бы хотел, чтобы основной интерфейс обновлялся после завершения загрузки. Фактически твиттерное приложение для iPhone делает это довольно хорошо.
Есть предложения?