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?

Scott McKenzie
  • 16,052
  • 8
  • 45
  • 70

3 Answers3

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];
    }];
});
yuhua
  • 1,239
  • 8
  • 18
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.

Constantine
  • 439
  • 6
  • 10
  • 1
    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. – Scott McKenzie Nov 28 '15 at 20:35
  • 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. – Constantine Nov 29 '15 at 21:06
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
    }];
}];
Glen Wong
  • 21
  • 2