How to call a method once the completion block fin

2020-08-05 10:39发布

I want to call the signUp method first, once I got the userID, I need to call the another method normalSignupMethod.

[ConnectionObj signUp:user];
[helper normalSignupMethod:dict];

signUp Method:

[MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
    // Sign up was successful
    // Store user id
    [SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:@"%@",response.data[@"id"]];   

} errorBlock:^(QBResponse *response) {
    // Handle error here
    NSLog(@" error in creating session %@", response.error);
    [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"SignUp to Chat error!", nil)];
}];

This I how I have called:

dispatch_group_t group = dispatch_group_create(); 
       dispatch_group_async(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {
    NSLog(@"Block1");
    [ConnectionObj signUp:user];
});

dispatch_group_notify(group,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ {

    NSLog(@"Group notify");
    [helper normalSignupMethod:dict];
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
});

Block 1 executed first, and then group notify called. But I'm getting the userID after the normalSignupMethod is finished. How to wait for a signUp method to get userID before calling the normalSignupMethod?

2条回答
Lonely孤独者°
2楼-- · 2020-08-05 11:21

You can call the normalSignupMethod once the signUp:successBlock request returns to successBlock

[MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
        // Sign up was successful
        // Store user id
        [SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:@"%@",response.data[@"id"]]; 

        //call the signup method 
        [helper normalSignupMethod:dict];

    } errorBlock:^(QBResponse *response) {
        // Handle error here
        NSLog(@" error in creating session %@", response.error);
        [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"SignUp to Chat error!", nil)];
    }];
查看更多
Deceive 欺骗
3楼-- · 2020-08-05 11:37

You can create a block with your signUp method like this and pass the Bool completion value to check is it called successfully or not. So change your method declaration like this.

-(void)signUp:(QBUser*)user andHandler:(void (^)(BOOL result))completionHandler;

And its definition

-(void)signUp:(QBUser*)user andHandler:(void (^)(BOOL result))completionHandler {
    [MYRequest signUp:user successBlock:^(QBResponse *response, QBUUser *user) {
        [SingletonClass sharedMySingleton].userID = [NSString stringWithFormat:@"%@",response.data[@"id"]];   
        completionHandler(YES);
    } errorBlock:^(QBResponse *response) {
       // Handle error here
        NSLog(@" error in creating session %@", response.error);
        [SVProgressHUD showErrorWithStatus:NSLocalizedString(@"SignUp to Chat error!", nil)];
        completionHandler(NO);
    }];
}

Now call this method like this.

[ConnectionObj signUp:user andHandler:^(BOOL result) {
    if(result) {
        [helper normalSignupMethod:dict];
    }
}];
查看更多
登录 后发表回答