GCD的并发队列? (iOS 4.2.1) [英] Concurrent Queue with GCD? (iOS 4.2.1)
问题描述
Iam遇到问题:
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0UL);
concurrentQueue
是 iOS 4.2.1(设备)上的nil
但是相同的代码在另一台运行iOS 5.0.1的设备上运行良好。
concurrentQueue
is nil
on iOS 4.2.1 (device) but the same code works perfectly on another device which is running iOS 5.0.1.
当我检查标题时它表示自iOS 4.0起可用,我做错了吗?
When I checked the header it says it's available since iOS 4.0, am I doing something wrong?
下面的代码从互联网上获取图像,并且在4.2.1之后的所有内容中都能很好地工作但在4.2.1中没有,任何想法为什么?你可以使用GCD以其他方式创建并发队列吗?
The code below fetches images from the internet and works great in everything after 4.2.1 but not in 4.2.1, any ideas why? Can you create a concurrent queue some other way using 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
仅适用于iOS 5.0及更高版本。
It appears DISPATCH_QUEUE_PRIORITY_BACKGROUND
is only available for iOS 5.0 and later.
DISPATCH_QUEUE_PRIORITY_BACKGROUND
发送给队列以后台优先级运行;在调度所有高优先级队列并且系统在优先级设置为后台状态的线程上运行项目之后,计划执行队列。这样的线程具有最低优先级,并且任何磁盘I / O都受到限制,以最小化对系统的影响。
适用于iOS 5.0及更高版本。
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.
如果用户正在运行iOS 4,您可以使用 DISPATCH_QUEUE_PRIORITY_LOW
然后使用 DISPATCH_QUEUE_PRIORITY_BACKGROUND
for iOS 5及以后。
In the case the user is running iOS 4 you could go with DISPATCH_QUEUE_PRIORITY_LOW
and then use DISPATCH_QUEUE_PRIORITY_BACKGROUND
for iOS 5 and later.
编辑
如果你有文件有点误导你在这种情况下不要仔细阅读。
The documentation is a little misleading if you don't read it closely in this case.
这篇关于GCD的并发队列? (iOS 4.2.1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!