Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS block with return value

I am using a block from Facebook SDK. It returns a dictionary. I want that dictionary as a return value of a method. I am trying to wrap my head around the whole block concept but need a nudge in the right direction.

The block: (the argument for the block is a string userFBid)

-(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID {

[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
                    parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(
                                          FBRequestConnection *connection,
                                          id result,
                                          NSError *error
                                          ) {
                          result = (NSDictionary*)result;

//return result;                              
       }];

}

How do i get the return value?

I have tried to google it, but i cant get my hand around it.

I would appreciate any pointer in the right direction.

EDIT: The main question is the following: I need to the completion handler to call a method in another class... how to do that?

like image 817
B-Man Avatar asked Apr 05 '14 16:04

B-Man


1 Answers

As the method startWithGraphPath is asynchronous, you can't code as if it was synchronous : it means no return value, because as soon as this method is called, your app will continue to execute to the next line, and won't wait for a returned value.

So, to keep this async, I assume you want to use the result of this in your own function, so call it in your completionHandler block:

[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
                             parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          [self myRockinFunction:result];
       }];

//Considering this function 
-(void)myRockinFunction:(NSDictionary*) fb_result{
    //Do stuff with fb_result
}

Edit

OK, I get it. Modify your method to accept a callback as a parameter :

-(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID andCallback:(void (^)(NSDictionary *))callback {

    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
                    parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:^(FBRequestConnection *connection,id result,NSError *error) {
         //You should treat errors first
         //Then cast the result to an NSDictionary
         callback((NSDictionary*) result); //And trigger the callback with the result
    }];
}

Then, in your other class, use another block to treat your result :

[YourHelperClass getMutualFBFriendsWithFBid:fbID andCallback:^(NSDictionary* result){
    //Use result how you wish
    //Beware, this is async too.
}];

Note : you should treat the error before triggering your callback.

Edit 2 (Help from other users appreciated)

Even better, you might try to pass a callback taking all the parameters (not tested, and not sure of the syntax. If someone can correct me, I'd appreciate):

-(NSDictionary*) getMutualFBFriendsWithFBid:(NSString*)fbID andCallback:(void (^)(FBRequestConnection *,NSDictionary *,NSError *))callback {

    [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"/%@/mutualfriends/%@", [[PFUser currentUser] objectForKey:kFbID],userFBid]
                    parameters:nil
                             HTTPMethod:@"GET"
                      completionHandler:callback()]; //Not sure here!
}

[YourHelperClass getMutualFBFriendsWithFBid:fbID andCallback:^(FBRequestConnection *connection,NSDictionary * result,NSError *error){
     //You could deal with errors here now
}];

Here's a reference on Apple's docs for deeper understanding.

like image 114
Bigood Avatar answered Sep 27 '22 19:09

Bigood