Search code examples
xcode4.3objective-c-blocksfacebook-ios-sdk

FBRequestConnection in a UIView does not link IBOutlets


I have a ViewController with so many information and, to better read the code, I split parts of this VC in two Views. So, I initialize these Views, set frames and add them as subviews to the ViewController.

These Views have a .xib connected and I'm able to link to all my IBOutlets. Until here, all ok but..

..in one of these Views I need to query Facebook, to retrieve some information. So I use the code:

[FBRequestConnection startWithGraphPath:@"myGraphApiQuery"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                 [myOutlet setTitle:@"Test"]; // here's the issue
}];

With this code myOutlet never will be set with Test title, seems the outlet's not linked. I don't understand why I'm not able to link to my outlet from block code completionHandler of FBRequestConnection...

If I try to set the Title from a selector called from ViewController, it's all ok..

Can someone explain me the reason of it? I'm still a objective-c beginner.


Solution

  • The callback from FBRequestConnection is invoked on a background thread. UIKit calls must be made on the main thread. Try this:

    [FBRequestConnection startWithGraphPath:@"myGraphApiQuery"
                                 parameters:nil
                                 HTTPMethod:@"GET"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              dispatch_async(dispatch_get_main_queue(), ^{
                                  [myOutlet setTitle:@"Test"];
                              });
    }];