【问题标题】:AFNetworking + download images in queue + cancel operationsAFNetworking + 在队列中下载图像 + 取消操作
【发布时间】:2012-03-05 17:41:52
【问题描述】:

我需要下载图像队列。 我首先创建了我的操作,然后使用 AFNetworking 的“入队”方法添加它们。

我有两个问题: 1)我没有为队列工作的进度条(我让它与自定义操作队列一起工作) 2)我没有找到在我想要的时候停止队列的解决方案

我创建了第一个操作来批处理并在数组中添加主题:

  while ((dict = [enumerator nextObject]))
  {    
    NSMutableURLRequest *request = [[MyHTTPClient sharedClient] requestWithMethod:@"GET" path:@"ws/webapp/services/pull_image" parameters:dict];

    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
imageProcessingBlock:nil                                                                                       cacheName:nil
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) 
    {
      NSLog(@"image : %@", [image description]);
      // process images
    }
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
    {
      // manage errors
    }];

    [operations addObject:operation];
  }

然后,我将操作排入队列:

     [[MyHTTPClient sharedClient] enqueueBatchOfHTTPRequestOperations:operations 
       progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) 
       {
           float percentDone = ((float)((int)numberOfCompletedOperations) / (float)((int)totalNumberOfOperations));
  [delegate syncServicesController:self updateProgressView:percentDone];
       } 
       completionBlock:^(NSArray *operations) 
      {
        //
      }];

所以,进度下载不起作用。 但是我可以看到 numberOfCompletedOperations 的进度...? 1,2,3,4,5...是否需要在主线程中强制刷新进度视图?

当我试图停止网络任务时:

- (void)cancelAllRequests
{
  [[MyHTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"ws/webapp/services/pull_image"];

}

我不明白如何停止请求队列...这似乎可行,但我有这个错误:-[NSBlockOperation request]: unrecognized selector sent to instance 0x16f54c70

【问题讨论】:

    标签: queue afnetworking


    【解决方案1】:

    这些实际上是在最后一两天修复的 :)

    继续更新到最新版本的 master,其中包括以下内容:

    cc2115e469:进度块现在默认调度到 main,就像 AFNetworking 中的所有其他完成块一样。这应该可以解决 UI 不在那里更新的任何问题。

    cac44aeb34:修复了NSBlockOperation 被发送request 的问题。 cancelAllHTTPOperationsWithMethod: 中有一个错误的假设,即所有操作都是 AFHTTPRequestOperation。唯一的缺点是它不会处理您的批处理操作。为此,您始终可以遍历 httpClient.operationQueue.operations 并选择您想要的。

    【讨论】:

    • 你好,马特,嗯,你是对的,我现在有一个干净的进度条!谢谢。对于 2),我真的不明白如何杀死所有操作。我在我的 iPad 应用程序中创建了一个同步引擎,它将向 Rails Web 应用程序发送 3 或 4 个不同的 URL,有时我需要一个队列来存储和执行操作。我对所有操作都使用了 AFHTTPClient 的子类。我的目标是在应用程序退出活动时(或者如果用户想要停止同步)终止所有类型的操作或队列。我在 wiki 中看到了您的代码,但我不明白最好的方法(杀死所有操作)。
    • 您可以在应用处于非活动状态时使用[httpClient.operationQueue cancelAllOperations][httpClient.operationQueue suspend],在应用重新启动时使用[httpClient.operationQueue resume]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2014-05-07
    相关资源
    最近更新 更多