0

给定以下代码,假设我连接到服务并获取一些项目,我需要更新标签以显示项目“处理”时的进度

问题是操作阻塞了主线程,并且 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]
4

1 回答 1

0

This kind of http request for json data will get response only once, and you can get all the data in the "JSON" object. So there is no need to show progress. Only in case that a request will receive multiple responses , you need to show the progress .For example ,a request for a webview.

于 2013-04-03T06:05:54.150 回答