如何获取sendSynchronousRequest的进度信息?

时间:2012-04-24 16:20:44

标签: objective-c synchronization nsurlconnection

我在第二个线程中调用sendSynchronousRequest,我的第二个线程将被阻塞,那么如何在我的第一个线程中获取进度信息?

1 个答案:

答案 0 :(得分:0)

您必须在第二个帖子上使用asynchronous downloader。然后异步使用委托回调!

例如:

    @protocol DownloadUIProtocol<NSObject>
    - (void)updateUI:(id)sender;
    @end

    @interface ViewController () {
      NSMutableData *receivedData_;
      NSDate *lastUpdateUITime_;
      __weak id<DownloadUIProtocol> delegate_;
    }
    @end

    ...

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
      [receivedData_ appendData:data];

      NSDate *now = [NSDate date];
      if (fabs([lastUpdateUITime_ timeIntervalSinceDate:now]) > 60) {
        dispatch_async(dispatch_get_main_queue(), ^{
          if ([delegate_ respondsToSelector:@selector(updateUI:)]) 
            [delegate_ updateUI:self];
        });
        lastUpdateUITime_ = now;
      }  
    }