Matthieu Lucas
Matthieu Lucas

Reputation: 817

Async FB request with block in separated thread issue

I'm working with the IOS Facebook SDK 3, and I'm trying to use it with the more efficient approach. So I would like to manage some requests in separate threads.

For example this request (WORKS PERFECTLY) :

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(queue, ^{

    [self generateShareContentFor:ShareServiceTypeFacebook 
                         callback:^(NSMutableDictionary* obj)
     {
         FBRequest * rq = [FBRequest requestWithGraphPath:@"me/feed" 
                                               parameters:obj 
                                               HTTPMethod:@"POST"];
         [rq startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { 

              dispatch_async(dispatch_get_main_queue(), ^{
                 // TREATING RESULT
                 [[UFBManager defaultManager] errorHandlerFromError:error 
                                                         fromRqType:UFBManagerRqTypePost];
              });

          }];
     }];  

});

This request doesn't work

     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
     dispatch_async(queue, ^{

          FBRequest * rq = [FBRequest requestForMe];
          [rq startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                  dispatch_async(dispatch_get_main_queue(), ^{

                    // TREATING RESULT
                    [[UFBManager defaultManager] errorHandlerFromError:error 
                                                          fromRqType:UFBManagerRqTypeGet];

                  });

          }];
     });

I'm trying to figure out, but I don't understand what is the problem. Thank's in advance for your help.

Upvotes: 6

Views: 6071

Answers (2)

user1951992
user1951992

Reputation:

I had this problem for a bit.

Make sure you dispatch the code on the main thread.

dispatch_async(dispatch_get_main_queue, ^{

           FBRequest * rq = [FBRequest requestForMe];
           [rq startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                   //The docs say this will be the main queue here anyway
                   //Feel free to go on a background thread at this point
                   }];

        });

Upvotes: 6

Christopher Pickslay
Christopher Pickslay

Reputation: 17762

I'm not positive why it works in one case and not the other, but I think it has to do with the run loop for your background queue not running after startWithCompletionHandler: returns.

But I wonder why you're putting that on a background queue anyways, since it's an asynchronous call. Why not just do this from the main thread:

FBRequest * rq = [FBRequest requestForMe];
[rq startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
      dispatch_async(dispatch_get_main_queue(), ^{
        [[UFBManager defaultManager] errorHandlerFromError:error 
                                              fromRqType:UFBManagerRqTypeGet];
      });
}];

Upvotes: 2

Related Questions