完成块完成后如何调用方法? [英] How to call a method once the completion block finish?

查看:122
本文介绍了完成块完成后如何调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想首先调用 signUp 方法,一旦我获得了userID,我需要调用另一个方法 normalSignupMethod

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方法:

[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)];
}];

这个我怎么称呼:

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];
    });
});

首先执行块1,然后调用group notify。但是我在normalSignupMethod完成后得到了userID。如何在调用normalSignupMethod之前等待signUp方法获取userID?

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?

推荐答案

您可以创建一个使用您的 signUp 方法阻止并传递 Bool 完成值以检查是否为叫成功与否。所以改变你的方法声明。

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;

及其定义

-(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];
    }
}];

这篇关于完成块完成后如何调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆