I'm getting acquainted with ASIHTTPRequest library and i tried to implement connection to a server. Here is my code:
- (BOOL)IsEnteredDataCorrect {
__block NSString *password;
__block NSString *responseString;
NSString *url = [NSString stringWithFormat:@"%@/login/",SERVER_URL];
__block ASIHTTPRequest *loginRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
hud.labelText = @"Please wait";
[loginRequest setCompletionBlock:^
{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
responseString = [loginRequest responseString];
// NSLog(@"%@",responseString);
NSData *responseData = [loginRequest responseData];
NSError *err = nil;
NSDictionary *users = [[CJSONDeserializer deserializer] deserializeAsDictionary:responseData error:&err];
password = [users objectForKey:idField.text];
}];
[loginRequest setFailedBlock:^
{
[MBProgressHUD hideHUDForView:self.navigationController.view animated:YES];
[delegate alertError:[loginRequest error]];
NSLog(@"failed block");
}];
[loginRequest startAsynchronous];
NSLog(@"request is over");
if ([password isEqualToString:passwordField.text])
return YES;
else
return NO;
}
The server name is fake so i expected setFailedBlock to get called. But strange thing happens: MBProgressHUD is taken away (it is possible only when one of the blocks is called) but the code in setFailedBlock is not executed. The further code is executed successfully. OK, setFailedBlock doesn't work - but then MBProgressHUD should stay on the screen and it's gone. Can anyone explain me what's going on?
If your failed block isn't running but the MBProgressHUD is vanishing then your completion block is running.
Perhaps your server name isn't as fake as you think it is? There's an awful lot of weird domain names registered...
Also, your code here:
[loginRequest startAsynchronous];
NSLog(@"request is over");
if ([password isEqualToString:passwordField.text])
return YES;
else
return NO;
You seem to have a fundamental mismatch here - your are using an asynchronous request, but appear to be expecting that the answer is there immediately.