给定以下代码,假设我连接到服务并获取一些项目,我需要更新标签以显示项目“处理”时的进度
问题是操作阻塞了主线程,并且 UILabel 只有在迭代所有项目后才会更新。
如何修复此代码,以便每次都会更新?
AFJSONRequestOperation *operation =
[AFJSONRequestOperationJSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
datas = [JSON valueForKeyPath:@"data"];
for(int i = 0; i < [datas count]; i ++)
{
//do stuff with the item
NSMutableString *progressText = [[NSMutableString alloc]
initWithFormat:@"Done with %d out of %@ items", i, [datas count]];
self.progressLabel.text = progressText;
}
}];
[operation start]