gpt4 book ai didi

iphone - AFnetworking waitUntilFinish 不工作

转载 作者:行者123 更新时间:2023-11-29 11:04:59 25 4
gpt4 key购买 nike

我正在制作一个使用 AFNetworking 库的项目。我用它来检查网络服务的登录名和密码。如果没问题,这会给我一个 200 代码。如果是 200,我将一个 bool 值设置为 true,以便应用程序可以继续。但是这个 bool 值没有在正确的时间设置。我总是需要在我的 login button 上按两次才能工作。这是我设置 bool 值的代码。

    - (BOOL)credentialsValidated {
self.progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.progressHUD.labelText = @"Loading";
self.progressHUD.mode = MBProgressHUDModeIndeterminate;
self.progressHUD.dimBackground = YES;
[[API sharedInstance] loginCommand:[NSMutableDictionary dictionaryWithObjectsAndKeys:_txtLogin.text,@"email",_txtPass.text,@"pwd", nil] onCompletion:^(NSDictionary *json){
//completion
if(![json objectForKey:@"error"]){
NSLog(@"status %@",[json valueForKeyPath:@"data.status"]);
if([[json valueForKeyPath:@"data.status"]intValue] == 200){
//Create user object


_loginstatus = YES;
[self.progressHUD hide:YES afterDelay:5];
}else{
//show validation
_txtLogin.text = @"";
_txtPass.text = @"";
_loginstatus = NO;
}
}else {
NSLog(@"Cannot connect to the server");
}
}];
if(_loginstatus){
NSLog(@"true");
}else{
NSLog(@"false");
}
[self.progressHUD hide:YES afterDelay:5];
return _loginstatus;
}

这是我的 API 代码。

-(void)loginCommand:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock{
NSLog(@"%@%@",kAPIHost,kAPILogin);
NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path:kAPILogin parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData>formData){
//TODO: attach file if needed

}];
AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){
//success !
NSLog(@"SUCCESSSS!");
completionBlock(responseObject);
}failure:^(AFHTTPRequestOperation *operation, NSError *error){
//Failure
NSLog(@"FAILUREE!");
completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];
[operation start];

}

当我在 [operation start] 下方放置 [operation waitUntilFinish] 代码时,应用程序崩溃了。

有人可以帮我解决这个问题吗?

亲切的问候

日志这是我在我 2 两次按下登录按钮后打印的日志

2012-12-28 09:36:00.547 Offitel[6532:907] http://virtuele-receptie.******.sanmax.be/nl/webservice/company-user/****/*****/**************
2012-12-28 09:36:00.561 Offitel[6532:907] false
2012-12-28 09:36:01.604 Offitel[6532:907] SUCCESSSS!
2012-12-28 09:36:01.605 Offitel[6532:907] status 200
2012-12-28 09:36:20.742 Offitel[6532:907] aanmelden pressed
2012-12-28 09:36:20.746 Offitel[6532:907] http://virtuele-receptie.******.sanmax.be/nl/webservice/company-user/****/*****/**************
2012-12-28 09:36:20.748 Offitel[6532:907] true
2012-12-28 09:36:22.184 Offitel[6532:907] SUCCESSSS!
2012-12-28 09:36:22.184 Offitel[6532:907] status 200

最佳答案

如果你这样做

return _loginstatus;

您没有等待操作完成,因此您没有返回服务器发送给您的应用程序的内容。在这种情况下,我通常做的是在触发登录操作时显示一个 UIActivityIndi​​cator,并且在服务器响应之前什么都不做。然后,我为任何变量设置适当的值,在这种情况下,我会转到主屏幕。

请记住,当您使用 AFNetworking 时,您总是在执行异步操作。

编辑

代码示例:

- (void)loginActionWithPassword:(NSString *)pass
{
// Add progress HUD to show feedback
// I'm using MBProgressHUD library: https://github.com/jdg/MBProgressHUD#readme
self.progressHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.progressHUD.labelText = @"Loading";
self.progressHUD.mode = MBProgressHUDModeIndeterminate;
self.progressHUD.dimBackground = YES;

// Launch the action to the server
[Actions loginWithEmail:[self.textEmail.text lowercaseString] andPassword:pass success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
if ([Actions requestFailed:JSON]) {
LOG(@"ERROR %@ / %@ / %@ \n", pass, request, response, JSON);
if ([JSON[@"MSG"] isEqualToString:@"USER_NOT_REGISTERED"]) {
self.progressHUD.labelText = @"User doesn't exist";
self.textEmail.text = @"";
self.textPassword.text = @"";
} else if ([JSON[@"MSG"] isEqualToString:@"PASSWORD_INCORRECT"]) {
self.progressHUD.labelText = @"Wrong password";
self.textPassword.text = @"";
}
[self.progressHUD hide:YES afterDelay:[Utils hudDuration]];
return;
}
// If everything went OK, go to this function to save user data
// and perform segue to home screen (or dismiss modal login view)
[self onLoginSuccessWithPassword:pass JSON:JSON response:response];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
// If something fails, display an error message
LOG(@"ERROR:(%@): %@ / %@ / %@ / %@ \n", pass, request, response, error, JSON);
self.progressHUD.labelText = @"Something went wrong, try again";
[self.progressHUD hide:YES afterDelay:[Utils hudDuration]];
return;
}];
}

关于iphone - AFnetworking waitUntilFinish 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14066441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com