我在第二个线程中调用了sendSynchronousRequest,我的第二个线程将被阻塞,那么如何在我的第一个线程中获取进度信息?
问问题
578 次
1 回答
0
您必须在第二个线程上使用异步下载器。然后异步使用委托回调!
例如:
@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;
}
}
于 2012-04-24T16:40:55.553 回答