完成块完成后如何调用方法?
我想先调用signUp
方法,一旦我得到了userID,我需要调用另一个方法normalSignupMethod
。
[ConnectionObj signUp:user]; [helper normalSignupMethod:dict];
注册方法:
[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先执行,然后组通知调用。 但是在正常的SignupMethod完成后,我得到了userID。 如何在调用normalSignupMethod之前等待signUp方法来获取userID?
你可以用你的signUp
方法创build一个block
,并传递Bool
完成值来检查是否调用成功。 所以像这样改变你的方法声明。
-(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); }]; }
现在就像这样调用这个方法。
[ConnectionObj signUp:user andHandler:^(BOOL result) { if(result) { [helper normalSignupMethod:dict]; } }];
一旦signUp:successBlock
请求返回到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)]; }];