Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concurrent Queue with GCD? (iOS 4.2.1)

Iam having problems with:

dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0UL);

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.

When I checked the header it says it's available since iOS 4.0, am I doing something wrong?

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];
                });
            }           
        });
    }); 
}
like image 358
Konrad77 Avatar asked Dec 07 '11 18:12

Konrad77


1 Answers

It appears DISPATCH_QUEUE_PRIORITY_BACKGROUND is only available for iOS 5.0 and later.

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

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.

Edit

The documentation is a little misleading if you don't read it closely in this case.

enter image description here

like image 191
Chris Wagner Avatar answered Oct 02 '22 20:10

Chris Wagner