0

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?

4

2 回答 2

2

You could try performing hte request like this and handle the rest from inside a block. The code inside the block will only when run when the request has returned.

So it could be something like this:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *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];    
}
else
{
    [SVProgressHUD dismiss];
}  

}];
于 2013-10-29T15:52:09.540 回答
1

Yes and this make complete sense, since the HUD is being displayed in the main thread, all UI animation are. Then you start the the request which is blocking the main thread.

You must either do the the request in the background, which makes sense for all network requests or delay the blocking call to give your UI time to update.

I really suggest that you use the asynchronous request, because this will give you app a more responsive nature even if you app needs to wait for a request to finish before doing anything else.

于 2013-10-29T15:44:30.940 回答