我想首先调用signUp
方法,一旦我获得了userID,我需要调用另一个方法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,然后调用组通知。但是我在normalSignupMethod完成后获得了userID。如何在调用normalSignupMethod之前等待signUp方法获取userID?
答案 0 :(得分:4)
您可以使用block
这样的方法创建signUp
并传递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];
}
}];
答案 1 :(得分:3)
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)];
}];