首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BGTaskScheduler:是否可以在后台任务中安排后台任务?

BGTaskScheduler:是否可以在后台任务中安排后台任务?
EN

Stack Overflow用户
提问于 2022-07-26 15:25:54
回答 1查看 263关注 0票数 1

假设一个应用程序在1小时后执行一个后台任务,但是当它执行时,它会发现用户没有互联网连接,所以它不能完成它的工作。是否可以在后台任务中安排另一个后台任务,以便在另一个小时后执行?

EN

回答 1

Stack Overflow用户

发布于 2022-07-26 17:34:22

是的,您可以在处理当前任务时安排下一个任务。

使用后台任务更新应用程序中的代码示例正是这样做的,将下一个任务(scheduleAppRefresh)调度为处理应用程序刷新的第一步:

代码语言:javascript
复制
func handleAppRefresh(task: BGAppRefreshTask) {
    // Schedule a new refresh task.
    scheduleAppRefresh()

    // Create an operation that performs the main part of the background task.
    let operation = RefreshAppContentsOperation()
   
    // Provide the background task with an expiration handler that cancels the operation.
    task.expirationHandler = {
        operation.cancel()
    }

    // Inform the system that the background task is complete
    // when the operation completes.
    operation.completionBlock = {
        task.setTaskCompleted(success: !operation.isCancelled)
    }

    // Start the operation.
    operationQueue.addOperation(operation)
}

还请参阅使用后台任务刷新和维护应用程序示例项目。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73126077

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档