应用程序关闭时在后台运行任务

问题描述

| 我正在创建一个应用程序,其中我使用保管箱sdk从保管箱下载大文件。下载功能的工作方式是我调用downloadFile方法并将其传递给委托,该委托将在文件开始下载时以及文件完全下载后回叫。 但是,现在,如果正在下载文件并且我关闭了该应用程序,则文件下载将暂停,直到用户返回该应用程序为止。 我尝试使用以下代码,但是当我关闭应用程序时,下载仍然无法完成,直到您返回应用程序。
UIApplication* app = [UIApplication sharedApplication];

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

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{


     //This call calls the sdk to start downloading the file. That method will then
     // call this classes delegate methods with the progress of the download as well
     // as when the file is totally finished downloading
     [DBUtils downloadFile:fileVO.filename withHash:fileVO.filehash withRestClient:self.restClient];


    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});
知道我该如何解决这个问题?     

解决方法

我猜DBUtils downloadFile:…是一种异步方法。在这种情况下,您正在做的事情是开始下载,然后立即结束后台任务。 您应该做的是在DBUtils上设置一个委托方法,以让您的类知道下载何时完成,然后从那里调用endBackgroundTask。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...