1

我的应用程序有一个将图像上传到网络服务器的功能。虽然这是在后台发生的,但我设置了一个进度条,以便用户可以看到上传状态。我正在上传进度条,如下所示。只要没有发出其他 HTTP 请求,一切正常。一旦发出另一个请求,setUploadProgressBlock 就会停止更新,但 setCompletionBlockWithSuccess 最终会被调用,所以我的进度条将在 30 到 70 之间的某个百分比,但实际上上传已经完成。对于另一个请求,我没有使用 AFNetworking。下面也有一个例子。我已经尝试调用 [operation waitUntillFinished] 以及我在网上看到的其他一些示例,但似乎没有任何效果。

图片上传 =

NSData *postData= [[NSString stringWithFormat:@"description=%@&location=%@&image=%@",i, l, d] dataUsingEncoding:NSNonLossyASCIIStringEncoding allowLossyConversion:YES];

NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@/upload/create", SERVICE_URL]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:postData];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:theRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);

        float percentDone = ((float)(totalBytesWritten) / (float)(totalBytesExpectedToWrite));

        [progressLabel setText:[NSString stringWithFormat:@"%.0f%%", (percentDone * 100)]];
        [progressView setProgress:percentDone];

}];

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

    NSLog(@"success: %@", operation.responseString);

}
  failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"error: %@",  operation.responseString);

  }
 ];
[httpClient enqueueHTTPRequestOperation:operation];

所有其他请求 =

NSURL *loginURL = [NSURL URLWithString: [NSString stringWithFormat:@"%@/user/add", SERVICE_URL]];

        NSData *postData= [[NSString stringWithFormat:@"user_id=%@", user_id] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:loginURL];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody:postData];

        NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        [NSURLConnection sendAsynchronousRequest:theRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
         {


         }];

更新:

为了解释更多,这里是一个控制台在这个过程中的样子的例子 -

已发送 437568 个字节,共 769069 个字节

发送了另一个 API 调用

进度条停止更新

另一个 API 调用响应

成功:图片上传响应

4

3 回答 3

1

您的未拥有的 HTTP 客户端实例可能在操作完成之前被释放。使用示例中显示的静态单例模式,或将其存储在strong某个拥有视图控制器的属性中。

于 2013-07-24T16:18:10.060 回答
0

切换到 ASIHTTPRequest ( http://allseeing-i.com/ASIHTTPRequest/ ) 以检索上传进度并解决问题!

于 2013-07-21T15:13:18.187 回答
0

setText:并且setProgress从错误的线程中调用,并且不更新是这里可能发生的危害最小的事情。

使用任一

dispatch_async(dispatch_get_main_queue(), ^ {
    [progressLabel setText:[NSString stringWithFormat:@"%.0f%%", (percentDone * 100)]];
    [progressView setProgress:percentDone];
});

或者

[progressLabel performSelectorOnMainThread:@selector(setText:) withObject:[NSString stringWithFormat:@"%.0f%%", (percentDone * 100)] waitUntilDone:NO];
[progressView performSelectorOnMainThread:@selector(setProgress:) withObject:(id)percentDone waitUntilDone:NO];
于 2013-07-21T01:30:03.043 回答