Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

share|improve this question
    
Another 'I didn't know this was async' question. – user529758 Nov 7 '12 at 21:46

2 Answers 2

up vote 2 down vote accepted

The problem is that the success and failure blocks 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 localerror is still nil and success is still NO.

share|improve this answer
    
Ok, thanks for the answer. I think I understand it now, but is there a solution for the problem? – digga Nov 8 '12 at 12:30
    
You need to refactor the code that calls your loving:withPassword:error:` method such that it doesn't depend on the return value. The success and failure blocks should call code that properly handles the result. – rmaddy Nov 8 '12 at 15:40

NSLog domain called out of block will be called earlier because it will be on the main thread, and NSLog in failure block fire in the new thread, and after the response from server. So return success also return not set success (No in your case).

share|improve this answer
    
Depending on the implementation of the AFKissXMLRequestOperation class, the success and failure blocks may actually be run on the main thread. – rmaddy Nov 7 '12 at 21:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.