0

在我的 iPhone 应用程序中处理用于存储和检索数据的 Web 服务。现在我正在使用以下代码进行 Web 服务处理。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

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

[request setTag:100];

[request setDelegate:self];

[request startAsynchronous];

通过这段代码,我得到了“requestFinished”方法的响应。我的问题是网络服务响应非常延迟(取决于互联网速度)。如何快速响应网络服务?请帮助我。

4

2 回答 2

1

我认为您想通过 post 方法发送 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];

}
于 2013-08-01T11:46:18.013 回答
0

当客户端使用您的应用程序时,由于网络提供商或环境不同,我们无法控制互联网速度。

但是你可以让你的网络服务在后台运行而不影响你的主要功能。

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.

    });
});
于 2013-07-30T08:07:45.503 回答