0

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.

1
  • Another 'I didn't know this was async' question. Commented Nov 7, 2012 at 21:46

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, thanks for the answer. I think I understand it now, but is there a solution for the problem?
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.
0

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).

1 Comment

Depending on the implementation of the AFKissXMLRequestOperation class, the success and failure blocks may actually be run on the main thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.