Asynchronous FB request with a block in a separate topic

I am working with the iOS Facebook SDK 3 and I am trying to use it with a more efficient approach. Therefore, I would like to manage some requests in separate threads.

For example, this query (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]; }); }]; }]; }); 
  • I use this to publish something in my channel, I call a method to load the contents of this request automatically, and then this method will be called in the request start method. It works well.

  • The problem is that I do not put this request in a block that does not work.

This request does not 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 am trying to find out, but I do not understand what the problem is. Thanks in advance for your help.

+6
source share
2 answers

I had this problem a bit.

Make sure you send the code to 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 }]; }); 
+6
source

I'm not sure why it works in one case and not in another, but I think this is due to the start loop for your background queue, which does not start after returning startWithCompletionHandler:

But I wonder why you put this in the background queue anyway, since this is an asynchronous call. Why not just do it 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]; }); }]; 
+2
source

Source: https://habr.com/ru/post/923923/


All Articles