微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/iOS/329475.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐