FBSDKGraphRequest in a bolts framework never calls block
Asked Answered
C

3

7

I have the following code:

[[[PFFacebookUtils logInInBackgroundWithAccessToken:[FBSDKAccessToken currentAccessToken]] continueWithSuccessBlock:^id(BFTask *task) {

    PFUser *user = task.result;

    return user;

}] continueWithSuccessBlock:^id(BFTask *task) {

    BFTaskCompletionSource *source = [BFTaskCompletionSource taskCompletionSource];

    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil];

    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

        if (error) {
            [source setError:error];
            return;
        }

        [source setResult:result];
    }];

    return source.task;
}];

The FBSDKGraphRequest works fine outside of the Bolts task, but inside the task the startWithCompletionHandler is not being called.

Any ideas?

Crosby answered 14/5, 2015 at 10:5 Comment(0)
S
20

I found a workaround. Just wrap it within a main thread block. It will work like a charm.

dispatch_async(dispatch_get_main_queue(), ^{
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil];

    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {

        if (error) {
            [source setError:error];
            return;
        }

        [source setResult:result];
    }];
});
Shoran answered 2/6, 2015 at 16:12 Comment(3)
This is a huge fix. Should have more upvotes. Thank you @yuhua!Timms
still not never calls the block of handler :(Pettaway
worked for me. I was calling this "initWithGraphPath" from background thread and apparently it should be only invoked from the main thread.Unlikelihood
L
2

We had the same exact issue and we used the same solution but I can not seem to find any posts which explain why this is happening.

Lodhia answered 27/11, 2015 at 19:47 Comment(2)
Now it's all open source we ought to be able to work out why. I'll revisit if I get the time and get back to you.Crosby
Thank you! Let me know if there is anything I can help with or look at myself. I might have some free time this week to help out.Lodhia
A
2

I ran into the same issue. It appears that PFFacebookUtils is executing its continuation block on a different thread but it appears that FBSDKGraphRequest expects to be started from the main thread. I found the issue can alternatively be solved by specifying an executor.

BFTask* loginTask = [PFFacebookUtils logInInBackgroundWithReadPermissions:@[]];
[loginTask continueWithExecutor:[BFExecutor mainThreadExecutor] withSuccessBlock:^id _Nullable(BFTask * _Nonnull task) {
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:nil];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
        // This code gets called properly
    }];
}];
Angel answered 9/12, 2015 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.