dispatch_queue_t concurrentQueue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_BACKGROUND,0UL);
在iOS 4.2.1(设备)上的concurrentQueue为零,但是在运行iOS 5.0.1的另一台设备上,相同的代码可以完美工作.
当我检查标题时,它表示从iOS 4.0起可用,我做错了什么?
下面的代码从互联网中获取图像,在4.2.1之后的所有内容中都能很好的工作,但是没有在4.2.1中,任何想法为什么?您可以使用GCD创建并发队列吗?
- (void)imageFromURL:(Nsstring*)link {
if ([link length] == 0)
return;
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(disPATCH_QUEUE_PRIORITY_BACKGROUND,0UL);
if (concurrentQueue == nil)
return;
dispatch_async(concurrentQueue,^{
__block UIImage* image = nil;
dispatch_sync(concurrentQueue,^{
NSError *error = nil;
NSURL *url = [[NSURL alloc] initWithString:link];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *imageData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil
error:&error];
if ( error == nil && imageData != nil) {
image = [UIImage imageWithData:imageData];
} else {
DLog(@"%@",[error description]);
}
if ([self.delegate respondsToSelector:@selector(setimage:)]) {
dispatch_sync(dispatch_get_main_queue(),^{
[self.delegate setimage:image];
});
}
});
});
}
解决方法
disPATCH_QUEUE_PRIORITY_BACKGROUND
Items dispatched to the queue run at background priority; the queue is scheduled for execution after all high priority queues have been scheduled and the system runs items on a thread whose priority is set for background status. Such a thread has the lowest priority and any disk I/O is throttled to minimize the impact on the system.
Available in iOS 5.0 and later.
Found here
在用户运行iOS 4的情况下,您可以使用disPATCH_QUEUE_PRIORITY_LOW,然后在iOS 5及更高版本中使用disPATCH_QUEUE_PRIORITY_BACKGROUND.
编辑
如果您在这种情况下不仔细阅读,文档有点误导.