2013-07-30 60 views
0

在我的iPhone應用程序處理存儲和檢索數據的Web服務。現在我使用下面的代碼進行Web服務處理。如何避免在iPhone中延遲webservice(ASIHTTPRequest)響應?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

[request setPostValue:@"1" forKey:@"id"]; 

[request setTag:100]; 

[request setDelegate:self]; 

[request startAsynchronous]; 

通過這個代碼我得到了「requestFinished」 method.My問題的反應是Web服務響應非常延遲(取決於網速)。如何實現從Web服務響應速度非常快?請幫我。

+1

那是你無法控制的東西。我想你很幸運,你在開發時遇到了這個問題,我們大多數人都在非常快的辦公室網絡上開發,當客戶端在所有類型的網絡問題上使用應用程序開始出現。您可以在後臺獲取數據,以便ui不會掛起。 – amar

+0

@ amar,你是絕對正確的!!如何在後臺調用webservice? – IKKA

+1

使用調度隊列http://www.raywenderlich.com/4295/multithreading-and-grand-central-dispatch-on-ios-for-beginners-tutorial和一個建議ASIHttp不再被維護遷移到網絡會話它會做背景中的東西給你 – amar

回答

0

當客戶端使用您的應用程序時,由於不同的網絡提供商或環境,我們無法控制互聯網的速度。

但是,您可以將您的Web服務在後臺運行而不影響您的主要功能。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    //code for webservices calling 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     //functions after your webservices done, for example reload the table or hide the loading bar. 

    }); 
}); 
1

我想你想通過郵寄method..delay發送JSON對象是依賴於你的服務器上(它是如何快速處理請求和響應回),但我建議你使用進度條和塊來處理網絡請求..

loadingHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
loadingHUD.labelText = NSLocalizedString(@"Downloading", nil); 
loadingHUD.mode=MBProgressHUDModeAnnularDeterminate; 
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject]; 
// Add your filename to the directory to create your saved file location 
NSString* destPath = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".mov"]]; 
NSURL *url = [NSURL URLWithString:mainURL]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:postURL parameters:postRequest]; 
NSLog(@"postRequest: %@", postRequest); 

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:destPath append:NO]; 



[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
{ 

     NSLog(@"Successfully downloaded file to %@",[[NSString alloc] initWithData:operation.responseData encoding:NSASCIIStringEncoding]); 

    // Give alert that downloading successful. 
    NSLog(@"Successfully downloaded file to %@", destPath); 

    NSLog(@"response: %@", operation.responseString); // Give alert that downloading successful. 

    // [self.target parserDidDownloadItem:destPath]; 

    loadingHUD.detailsLabelText = [NSString stringWithFormat:@"%@ %i%%",@"Downloading",100]; 
    [loadingHUD hide:TRUE]; 

    [DBHelper savePurchaseId:fileName]; 
    [self movieReceived]; 

} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{ 
    // Give alert that downloading failed 
    NSLog(@"Error: %@", error); 

    // [self.target parserDidFailToDownloadItem:error]; 
    [loadingHUD hide:TRUE]; 

}]; 
[operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) 
{ 
    // Progress 
    progress = ((float)totalBytesWritten)/fileSize; 
    loadingHUD.progress = progress; 
      }]; 
[operation start]; 

}