SVProgressHUD的性能问题

时间:2013-10-29 15:40:57

标签: ios svprogresshud

我有一段代码正在产生浇筑性能而不能按预期工作。它利用来自https://github.com/samvermette/SVProgressHUD的以下github存储库的SVProgressHUD。我有一个代码区域,我需要使用[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]。我希望在同步请求之前显示进度hud,然后在请求完成后解除。以下是有问题的代码:

//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;
}  

由于某种原因,同步请求阻止显示HUB。它在同步请求发生后立即显示,不幸的是,这也是在它被解雇时。显示给用户的行为是应用程序挂起,等待同步请求完成,快速闪烁HUB然后再次响应。我该如何解决这个问题?我希望在此挂起时间内显示SVProgressHUB,我该怎么做?

2 个答案:

答案 0 :(得分:2)

您可以尝试执行这样的hte请求,并从块内部处理其余的请求。块中的代码仅在请求返回时运行。

所以它可能是这样的:

[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];
}  

}];

答案 1 :(得分:1)

是的,这是完全合理的,因为HUD正在主线程中显示,所有UI动画都是。 然后启动阻止主线程的请求。

您必须在后台执行请求,这对所有网络请求都有意义,或者延迟阻止调用以使您的UI有时间更新。

我真的建议您使用异步请求,因为即使您的应用需要在执行任何其他操作之前等待请求完成,这将为您提供更具响应性的应用。