GCD并发队列? (iOS 4.2.1)

前端之家收集整理的这篇文章主要介绍了GCD并发队列? (iOS 4.2.1)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Iam有问题:
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仅适用于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.

Found here

用户运行iOS 4的情况下,您可以使用DISPATCH_QUEUE_PRIORITY_LOW,然后在iOS 5及更高版本中使用DISPATCH_QUEUE_PRIORITY_BACKGROUND.

编辑

如果您在这种情况下不仔细阅读,文档有点误导.

原文链接:https://www.f2er.com/iOS/329475.html

猜你在找的iOS相关文章