2013-10-29 9 views
0

我有一段代碼可以產生傾注性能,但不能按預期工作。它利用來自以下github存儲庫中的SVProgressHUD:https://github.com/samvermette/SVProgressHUD。我有一個代碼區域,我需要使用[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]。我想在同步請求之前顯示進度hud,然後在請求完成後解除。以下是有問題的代碼:SVProgressHUD的性能問題

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

你可以嘗試像這樣執行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

是的,這是完全合理的,因爲HUD顯示在主線程中,所有的UI動畫都是。 然後你開始阻塞主線程的請求。

您必須在後臺執行請求,這對所有網絡請求都有意義,或者延遲阻止呼叫以給您的UI更新時間。

我真的建議您使用異步請求,因爲即使您的應用程序需要等待請求完成任何其他操作之前,它也會爲您提供更具響應性的應用程序。

+0

的問題是,我需要返回一個值形成的功能。當請求異步執行並使用編譯頭時,我無法做到這一點。 @rckoenes – ScottOBot

+0

你的設計是錯誤的,viewController應該顯示和隱藏HUD而不是你的方法。由於它阻止主線程,HUD將不會顯示。 – rckoenes

+0

我將方法切換爲void,將依賴於返回的BOOL的代碼移植到方法內部,並將請求切換到異步。感謝您的幫助,我希望我可以爲您自己和Zoltan提供答案,但這是他使用的代碼。感謝大家@Zoltan Varadi的幫助 – ScottOBot