Reputation: 817
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];
});
}];
}];
});
I'm using this one to post something on my feed, I call a method to load the content of this request automatically and then this block will be called in the method to launch the request. This one works well.
The problem is if I don't put this request inside a block, that doesn't work.
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
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
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