I have a section of code that is producing pour performance and not working as expected. it utilizes the SVProgressHUD from the following github repository at https://github.com/samvermette/SVProgressHUD. I have an area of code where I need to use [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]
. I want to have a progress hud displayed before the synchronous request and then dismissed after the request has finished. Here is the code in question:
//Display the progress HUB
[SVProgressHUD showWithStatus:@"Signing Up..." maskType:SVProgressHUDMaskTypeClear];
NSError* error;
NSURLResponse *response = nil;
[self setUserCredentials];
// create json object for a users session
NSDictionary* session = [NSDictionary dictionaryWithObjectsAndKeys:
firstName, @"first_name",
lastName, @"last_name",
email, @"email",
password, @"password",
nil];
NSData *jsonSession = [NSJSONSerialization dataWithJSONObject:session options:NSJSONWritingPrettyPrinted error:&error];
NSString *url = [NSString stringWithFormat:@"%@api/v1/users.json", CoffeeURL];
NSURL *URL = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonSession length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:jsonSession];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *JSONResponse = [NSJSONSerialization JSONObjectWithData:[dataString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = httpResponse.statusCode;
NSLog(@"Status: %d", statusCode);
if (JSONResponse != nil && statusCode == 200)
{
//Dismiss progress HUB here
[SVProgressHUD dismiss];
return YES;
}
else
{
[SVProgressHUD dismiss];
return NO;
}
For some reason the synchronous request blocks the HUB from being displayed. It displays immediately after the synchronous request happens, unfortunatly this is also when it is dismissed. The behaviour displayed to the user is that the app hangs, waits for the synchronous request to finish, quickly flashes the HUB and then becomes responsive again. How can I fix this issue? I want the SVProgressHUB
to be displayed during this hang time, how can I do this?