Question

I am attempting to create a C++ bridge for the IOS facebook SDK. I can log in fine, but when I try to retrieve the user details the graph functions never seem to execute though I can step over them in the debugger. Here's what I have:

void facebook_bridge::performLoginWithFacebook()
{
    // Open a session showing the user the login UI
    // You must ALWAYS ask for basic_info permissions when opening a session
    [FBSession openActiveSessionWithReadPermissions:@[@"basic_info", @"email"]
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session, FBSessionState state, NSError *error) {

         // Retrieve the app delegate
         AppController* appDelegate = (AppController*)[UIApplication sharedApplication].delegate;
         // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
         [appDelegate sessionStateChanged:session state:state error:error];
     }];
    isLoggedIn = true;

}

cocos2d::CCDictionary* facebook_bridge::getUserDetails()
{
    __block NSDictionary *currentPermissions;
    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  // These are the current permissions the user has
                                  currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                  NSLog(@"No error");
                                  // We will store here the missing permissions that we will have to request
                                  // If we have permissions to request
                                                                } else {
                                  NSLog([NSString stringWithFormat:@"error %@", error.description]);
                              }
                          }];
    cocos2d::CCDictionary* dict = cocos2d::CCDictionary::create();
    for(int i = 0; i < currentPermissions.count; i++)  //get a bad access here, currentPermissions is always null
    {
        NSString* val = currentPermissions.allValues[i];
        NSString* k = currentPermissions.allKeys[i];
        std::string value = *new std::string(val.UTF8String);
        std::string key = *new std::string(k.UTF8String);
        cocos2d::CCString *ccVal = cocos2d::CCStringMake(value);
        dict->setObject((cocos2d::CCObject*)ccVal, key);
    }
    dict->retain();
    return dict;
}

I think the FBRequest is asynchronous, so I tried placing it in the first function to run on the same thread, to no avail.

Any help? (I'm very new to objective C, but have experience in most other languages)

Was it helpful?

Solution

I think the FBRequest is asynchronous, so I tried placing it in the first function to run on the same thread, to no avail.

I think you are completely missing the meaning of "asynchronous". You don't have to have threads for things to be asynchronous -- you can execute something asynchronously on the same thread. (Though in this case it probably does its main stuff on another thread, and then runs the completion block on the original thread.)

When you call an asynchronous API, the completion block will not execute before the rest of your function. Pretty much guaranteed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top