【问题标题】:AFNetworking background download for big fileAFNetworking后台大文件下载
【发布时间】:2013-08-12 10:40:04
【问题描述】:

我正在使用 Afnetworking 处理我的项目,我想在后台下载大约 100mb 或 150mb 的大文件,但在苹果文档中他们说后台任务将持续长达 10 分钟,那么我应该如何解决这个问题?

我在互联网上搜索并idle timer 禁用它或根据文件设置计时器?(但我如何根据我的文件大小设置空闲计时器,这也取决于互联网连接)..

在某些 SO post matte 中发布此答案,但我不确定他是否为此 afnetworking background answer 禁用空闲计时器

(void)applicationWillResignActive:(UIApplication *)application {
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
    [application endBackgroundTask:backgroundTaskIdentifier];
    [[YourRestClient sharedClient] cancelAllHTTPOperations];
}];

这是我的afnetworking代码

        NSURL *url = [NSURL URLWithString:downloadMainURL];

        AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
        [httpClient.operationQueue setMaxConcurrentOperationCount:1];


        NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:downloadPostUrl parameters:postRequest];

        AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:destPath shouldResume:YES];


        [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            if(operation.response.statusCode == 200) {

                 NSLog(@"enable ipad sleep");
                [[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
                 [loadingHUD hide:TRUE];
              //   NSLog(@"responseString is %@",[operation responseString]);
                              }
            else{
                NSLog(@"setCompletionBlockWithSuccess");
                [[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
                [loadingHUD hide:TRUE];
            }

        }failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            if(operation.response.statusCode!= 200) {

                [[UIApplication sharedApplication] setIdleTimerDisabled:NO];// enable the idle screen timmer
                [loadingHUD hide:TRUE];
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Attention" message:@"download failed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alertView show];
            }
        }];

        [httpClient enqueueHTTPRequestOperation:operation];

        [operation setDownloadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite)
         {

             int filesize = [[NSUserDefaults standardUserDefaults] integerForKey:@"filesize"];
             int totalbytes=filesize+bytesWritten;


             NSLog(@"productidFileSize value is %f",productidFileSize);
             NSLog(@"totalBytesWritten value is %d",totalbytes);

             progress = ((float)totalbytes )/ productidFileSize;
             NSLog(@"progress value is %f",progress);
             loadingHUD.progress = progress;
             [[NSUserDefaults standardUserDefaults] setInteger:totalbytes forKey:@"filesize"];
                }];

    }


}

【问题讨论】:

  • @Boa 你有什么想法吗?
  • 我不使用 AFNetworking,抱歉!
  • @BoA 哪个库支持大文件后台下载?或者有没有其他方法可以在后台下载大文件?

标签: ios download afnetworking background-process


【解决方案1】:

没有办法“让 iOS 延长 10m 的限制”。

我相信你有两个选择:

  • 首先,您可以尝试围绕“resume”参数进行游戏,并在允许的时间内尽可能多地下载,然后当计时器到时,您会触发新的后台下载,从之前的下载中恢复。 请注意,您不知道允许多长时间,可以是 10 分钟或 1 分钟。对此没有任何保证。
  • 其次,iOS7 中新增了一种“后台模式”,称为“获取”。但同样,它将优先考虑“小而合理”的下载,而不是更大的下载。所以你下载的越多,惩罚就越高。

除此之外,您还需要在您的 UI 中使用加载器执行此操作,并要求您的用户进行下载。

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-10
      相关资源
      最近更新 更多