iphone - AFNetworking setUploadProgressBlock 停止更新

标签 iphone ios afnetworking

我的应用程序有一个将图像上传到网络服务器的功能。虽然这是在后台发生的,但我设置了一个进度条,以便用户可以看到上传状态。我正在上传进度条,如下所示。只要没有其他 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调用响应

成功:图片上传响应

最佳答案

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

关于iphone - AFNetworking setUploadProgressBlock 停止更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17767776/

相关文章:

ios - 在 iPhone 8 和运行 iOS11 的 iPhone 8 上使用 MFMessageComposeViewController 的问题

ios - Cocoapods - 框架 header 中缺少类

ios - AFNetworking:如何让 AFHTTPClient 处理 json 和 xml 响应?

ios - AFNetworking 使用 URL 设置 UITableViewCell 的 imageView 图像,设置图像后 imageview 框架仍然为 0

iphone - 如何将 KVO 添加到 MPMoviePlayer currentPlaybackTime?

ios - 如何同时关闭并弹出到 View Controller

ios - scrollViewWillBeginDragging 不起作用

ios - 在 Xcode 11.5 中,我尝试下载所需的配置文件。帐户屏幕上的代理和用户有什么区别?

ios - 在单个 UIActivityViewController 中为不同的服务提供不同的内容

objective-c - AFNetworking 如何检查上传操作是否完成或失败