How can i incorporate ASIHTTPRequest with Blocks into an GCD Concurrent Queue
Currently i am doing using this way. I don’t want to compare GCD Queues and ASINetworkQueue. Here i am using GCD Queues and want to know is i am doing correct
NSString *urlString = [NSString stringWithFormat:@"http://mysite.com/news_detail/",tagID];
NSURL *url = [NSURL URLWithString:urlString];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setDelegate:self];
[request setCompletionBlock:^{
dispatch_queue_t JSONProcessingQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(JSONProcessingQueue, ^{
dispatch_sync(JSONProcessingQueue, ^{
NSData *responseData = [request responseData];
[self processData:responseData];
});
dispatch_sync(dispatch_get_main_queue(), ^{
[self DisplayNews];
});
});
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Error in downloading : %@", error.localizedDescription);
}];
[request startAsynchronous];
I think my answer will help with your issue – Here i am trying to call my ASIHTTP request in a GCD . But Completion block and failed blocks are not executing
In short – don’t use GCD. use ASINetworkQueue.
EDIT:
Jonathan, I wasn’t comparing. Your current solution is just incorrect.
What you’re doing right now, is starting an asynchronous request, which is an operation performed on a different thread. In the completion/error blocks, which will be performed on the same different thread, you ask for some global queue (i.e. thread), and move all operations to it.
The problem is that GCD is not magic, just a very successful ambiguation of threading. It assumes that it starts on the main thread, and moves back and forth between threads.
If you’re really bent on learning the hard way, then here are my thoughts on your usage of GCD:
I assume you’re trying to get the data, parse it and update the GUI using the DisplayNews method. if so, and also assuming you must use a global queue and not create a queue of your own, This is how you should do it: