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 b
You already have it :)
I would write a method to process the dictionary, in order to keep the completionHandler block a little cleaner--but you could write your response-handling code inside the block. As another commenter mentioned, this is async so you're not really "returning" anything...you're handling the completion block when it gets called.
To help you understand a little, the completionHandler block in this case is a chunk of code that you're passing to the method as an argument, for it to call later. In essence, "whenever this call comes back, do this: ^{ ". The implementation of the FBRequest method will call your completionHandler (whatever that may be).
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
}
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.
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.