【问题标题】:How to get download progress with AFHTTPSessionManager in AFNetworking 3.0如何在 AFNetworking 3.0 中使用 AFHTTPSessionManager 获取下载进度
【发布时间】:2016-02-19 04:19:15
【问题描述】:

当我使用 AFNetworking 2 时,我可以像这样使用 AFHTTPRequestOperation 取得进展:

NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:aURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:_timeoutInterval];
AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
imageRequestOperation.responseSerializer = [AFImageResponseSerializer serializer];

__weak AFHTTPRequestOperation *imageRequestOperationForBlock = imageRequestOperation;

[imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    // ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ...
}];

[imageRequestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
    if (progress) {
        progress((float)totalBytesRead / totalBytesExpectedToRead);
    }
}];

我使用AFHTTPSessionManager GET:parameters:success:failure:获取数据,但不知道如何在AFNetworking 3.0中获取下载进度。

【问题讨论】:

标签: ios objective-c afnetworking


【解决方案1】:

cmets 中的链接具有误导性 (NSURLSessionDownloadTask)。对此感到抱歉。

下面的代码应该可以工作。

假设:此代码放置在 AFHTTPSessionManager 子类中,并声明了 NSURLSessionDataTask *testTask ivar。它应该很容易根据需要进行修改。

取自this answer的重要部分代码

- (void)test
{
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://eoimages.gsfc.nasa.gov/images/imagerecords/78000/78797/nwpassage_tmo_2012199_lrg.jpg"]];

    testTask = [self dataTaskWithRequest:request
                       completionHandler:^(NSURLResponse * __unused response, id responseObject, NSError *error) {

                           if (error)
                           {
                               //...
                               NSLog(@"error");
                           }
                           else
                           {
                               //...

                               UIImage *result = responseObject;

                               NSLog(@"Success: %@",NSStringFromCGSize(result.size));
                           }
                       }];

    [self setDataTaskDidReceiveDataBlock:^(NSURLSession *session,
                                                      NSURLSessionDataTask *dataTask,
                                                      NSData *data)
     {
         if (dataTask.countOfBytesExpectedToReceive == NSURLSessionTransferSizeUnknown)
           return;

         if (dataTask != testTask)
           return;

         NSUInteger code = [(NSHTTPURLResponse *)dataTask.response statusCode];

         if (!(code> 199 && code < 400))
           return;

         long long  bytesReceived = [dataTask countOfBytesReceived];
         long long  bytesTotal = [dataTask countOfBytesExpectedToReceive];

         NSLog(@"... %lld/%lld",
               bytesReceived,
               bytesTotal);
     }];

    [testTask resume];
}

【讨论】:

  • 太棒了!我之前在文档中看到过setDataTaskDidReceiveDataBlock,但我不知道它是做什么用的。非常感谢。
猜你喜欢
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
  • 2017-06-09
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多