I am currently struggeling why the following code doesnt log my used NSString.
-(BOOL)login:(NSString *)email withPassword:(NSString *)password error:(NSError *__autoreleasing *)error
{
__block BOOL success = NO;
if (*error)
return success;
__block NSString *domain = @"de.FranzBusch.Searchlight.ErrorDomain";
__block NSError *localerror = nil;
//HTTP Request
NSURL *url = [NSURL URLWithString:@"Wrong URL to test the failure block"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
email, @"email",
password, @"password", nil];
NSMutableURLRequest *request = [client requestWithMethod:@"POST" path:@"" parameters:params];
AFKissXMLRequestOperation *operation = [AFKissXMLRequestOperation XMLDocumentRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, DDXMLDocument *XMLDocument)
{
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *HTTPError, DDXMLDocument *XMLDocument)
{
localerror = [NSError errorWithDomain:domain code:-101 userInfo:[self generateErrorDictionary:@"HTTPError"]];
success = NO;
}];
[operation start];
NSLog(@"test %@", [localerror domain]);
return success;
}
If I try to log the domain inside the failure block I get the right one. But outside the scope the block variable isnt modified. Ans hints on what I understood wrong are appreciated.
The problem is that the
successandfailureblocks are called long after your `login:withPassword:error: method has completed and returned because the operation is done in the background.When your NSLog statement is reached, the operation isn’t finished yet and neither block has been executed yet. So
localerroris stillnilandsuccessis still NO.