Ios AFNetworking后台下载自动停止一段时间后,我需要恢复它

Ios AFNetworking后台下载自动停止一段时间后,我需要恢复它,ios,objective-c,afnetworking,Ios,Objective C,Afnetworking,我使用AFNetwork(它基于NAFDownloadRequestOperation)和我的任务从amazon bucket一个接一个地下载多个zip文件。 当应用程序在前台时,一切都运行得很好,但当应用程序进入后台模式时,时间下载只会运行一段时间,它会自动停止。我读了一些关于它的博客,在下载之前我会调用下面的方法 [self.operationUpdate setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{

我使用AFNetwork(它基于NAFDownloadRequestOperation)和我的任务从amazon bucket一个接一个地下载多个zip文件。 当应用程序在前台时,一切都运行得很好,但当应用程序进入后台模式时,时间下载只会运行一段时间,它会自动停止。我读了一些关于它的博客,在下载之前我会调用下面的方法

 [self.operationUpdate setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{
                NSLog(@"downloading will stop");
            }];
  • 问题在后台模式下,下载将自动停止
  • 我想要什么:如果在后台停止下载,当应用程序再次出现在前台时,我需要从该点继续下载
我在AppDelegate中也使用了以下代码,但我不知道如何恢复以前的下载

__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
        [application endBackgroundTask:backgroundTaskIdentifier];
        NSLog(@"applicationWillResignActive");

        [__SERVER_INSTANCE cancellAllDownloading];

     //   [[YourRestClient sharedClient] cancelAllHTTPOperations];
    }];
如果有任何解决方案,请提前告诉我,谢谢。

您应该使用

您的请求看起来像

AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Successfully downloaded file to %@", path);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operations addObject:operation];
重新启动应用程序并生成具有相同url的请求后,它将继续下载。“shouldResume:是”有效

所以,在您的后台任务中,您可以重新创建请求以完成下载

__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
        [application endBackgroundTask:backgroundTaskIdentifier];
        NSLog(@"applicationWillResignActive");

        [__SERVER_INSTANCE cancellAllDownloading];

     //   recreate here your request to finish fownload, 
     //or recreate in when app will enter foreground
    }];

希望这对您有所帮助

后台下载停止,因为过了一段时间,您可能没有在xcode中启用后台提取功能

请参阅附件中的截图

如果你写了这段代码

-(BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return true;
}
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
 completionHandler(UIBackgroundFetchResultNewData);
}


希望这会有帮助

在这里重新创建完成fownload的请求?这意味着我需要取消下载,对吗?你有大约10分钟的时间在后台执行操作。如果您知道,该请求将在此时间之前完成-您可以继续下载。否则,调用operations.suspendAll暂停,当您进入前台时,使用相同的参数进行AFDownloadRequestOperation-它将在下载之前恢复下载停止控件进入setShouldExecuteAsBackgroundTaskWithExpirationHandler此方法。如何在此处暂停,因为我尝试在此方法中暂停下载,但无法暂停。AFJSONRPCClient具有属性operationQueue。调用sunspendAll停止所有操作您可以创建自己的下载队列并将apiClient设置为singleton,这样您就可以在任何需要的地方访问它。我有启用后台提取选项,但不使用以下两种方法,直到出现问题为止,他们都在做什么:(
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication  *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask;

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];



        NSTimeInterval ti = [[UIApplication sharedApplication]backgroundTimeRemaining];
        NSLog(@"backgroundTimeRemaining: %f", ti);

        // just for debug
    }];
}