سؤال

I have been using the following structure in my projects for consuming API data:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    // SYNCHRONOUS network request
    // Data processing
    dispatch_async(dispatch_get_main_queue(), ^{
        // UI update
    });
});

On the other hand, I have seen quite frequently another structure where the network request is asynchronous (i.e. using AFNetworking) and then the data processing and UI update are handled in the completion block (which is not async - I think). Here is an example of what I am saying:

NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  // Data processing
  // UI update
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  // Error handling
}];

[operation start];

So, my questions are:

  1. In the second structure there the data processing is not being run asynchronously, it is?
  2. Why are we generally encouraged to run async network requests instead of sync ones from async blocks/threads.
  3. Why the second structure is much more known and spread than the first?

Is there something that I am missing?

هل كانت مفيدة؟

المحلول

In my opinion async calls are much more elegant solutions for networking in any language.

  1. Yes data processing is done in main thread in that example. Parsing strings and creating objects in UI isn't really a cpu cycle consuming job. However lets say you download an image and you want to process it, you shouldn't do it in this block (maybe spawn another thread). But image processing is another scope.it isn't related to the network code so network code is working as intended.
  2. With async network calls, you can cancel/pause the request. But in sync network calls you cannot do that.
  3. First of all gcd is new to the objective-c (i am not really sure but it was available after iOS 4. correct me if i am wrong). Before that we were using delegation and it was a lot of boilerplate codes. But with the second approach you can easily manage the networking code.

I hope this helps.

نصائح أخرى

In the second structure there the data processing is not being run asynchronously, it is?

No its not. Its run on the main thread. But the time consuming task of fetching data from remote server is already done on another thread. Once the data is retrieved, data processing will not take much time generally and can be run on the main thread. In rare cases(i have not come across any such) if data processing itself takes time and appears to block UI, then we can use NSOperations or GCD queues to process them.

This is achieved by making use of blocks which is just like a callback function.

Why the second structure is much more known and spread than the first?

Its easier to implement and also to understand once you know the syntax for blocks ;)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top