I have the following code that fetches some data from a php script. I’m finding that it blocks the interface so that the progress indicator doesn’t spin.
Is there an easy way to send this to the background so that the UI is free to do whatever it needs? I was under the impression that using blocks would have achieved this but I was obviously wrong (learning Obj-C one step at a time!)
Thanks!
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
self.user.username, @"username",
self.user.password, @"password",
nil];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
[NSURL URLWithString:@"http://domain.com"]];
// Show animated progress indicator, etc
[self enbleLoadingState:YES];
[client postPath:@"/en/api/login" parameters:params success:^(AFHTTPRequestOperation *operation, id response) {
[self enbleLoadingState:NO];
// Do stuff
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[self enbleLoadingState:NO];
// Show error
}];
This code should work, the request loads in the background. The problem must be somewhere else.
I don’t know where the time is spent on the main thread, maybe in
enbleLoadingState:? The method starting the request should return immediately. Test this by inserting a NSLog at the start and the end of the method and inside the success-block. The NSLog in the success-block should print out last.