假设一个应用程序在1小时后执行一个后台任务,但是当它执行时,它会发现用户没有互联网连接,所以它不能完成它的工作。是否可以在后台任务中安排另一个后台任务,以便在另一个小时后执行?
发布于 2022-07-26 17:34:22
是的,您可以在处理当前任务时安排下一个任务。
使用后台任务更新应用程序中的代码示例正是这样做的,将下一个任务(scheduleAppRefresh)调度为处理应用程序刷新的第一步:
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)
}还请参阅使用后台任务刷新和维护应用程序示例项目。
https://stackoverflow.com/questions/73126077
复制相似问题