iOS block with return value

前端 未结 2 634
孤独总比滥情好
孤独总比滥情好 2021-02-09 04:44

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

2条回答
  •  耶瑟儿~
    2021-02-09 05:30

    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.

提交回复
热议问题