鉴于以下代码,假设我连接到服务并获取某些项目,我需要更新标签以显示项目“处理”时的进度
问题是操作阻塞了主线程,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]
答案 0 :(得分:0)
这种对json数据的http请求只会获得一次响应,并且您可以获取“JSON”对象中的所有数据。所以没有必要显示进度。只有在请求将收到多个响应的情况下,您才需要显示进度。例如,请求Webview。