简体   繁体   English

完成块完成后如何调用方法?

[英]How to call a method once the completion block finish?

I want to call the signUp method first, once I got the userID, I need to call the another method normalSignupMethod . 我想先调用signUp方法,一旦获得用户ID,就需要调用另一个方法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. 首先执行块1,然后调用组通知。 But I'm getting the userID after the normalSignupMethod is finished. 但是我在normalSignupMethod完成后得到了userID。 How to wait for a signUp method to get userID before calling the normalSignupMethod? 如何在调用normalSignupMethod之前等待signUp方法获取用户ID?

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. 您可以使用signUp方法创建一个block ,并传递Bool完成值以检查其是否成功调用。 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];
    }
}];

You can call the normalSignupMethod once the signUp:successBlock request returns to successBlock 您可以拨打normalSignupMethod一旦signUp:successBlock请求返回到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)];
    }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 等待异步调用在完成块中完成 - Waiting for an asyncronous call to finish within a completion block SceneKit:如何一起为多个SCNNode制作动画,然后一次调用完成块 - SceneKit: how to animate multiple SCNNodes together then call completion block once 等待带有完成块的方法在快速枚举块内完成 - Wait for method with completion block to finish inside fast enumeration block 点击按钮后调用方法完成块 - Call a method completion block after button is tapped 目标C:有没有一种方法可以在另一个方法中调用一个方法的完成块? - Objective C: Is there a way to call a completion block for a method in another method? 如何使用块完成处理程序实现类方法 - How to implement a class method with a block completion handler 在块完成时调用presentModalViewController - call presentModalViewController in block completion 调用块方法并等待用户完成然后得到响应 - Call block method and wait for user to finish then get respond back 动画完成块在完成之前运行? - Animation completion block run prior to finish? 如何等待直到异步调用完成,包括完成块(AFNetworking) - How to wait until a async call complete, including completion block (AFNetworking)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM