【问题标题】:How to execute a task even when the application is in background or lock mode till it finishes即使应用程序处于后台或锁定模式,如何执行任务直到它完成
【发布时间】:2017-12-21 07:36:17
【问题描述】:

下面的代码我曾经在后台模式下编写某些任务。但这仅在任务结束后执行 3 分钟。所以我想执行特定的任务直到它完成。

func registerBackgroundTask() {
    backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
        self?.endBackgroundTask()   
    }
    assert(backgroundTask != UIBackgroundTaskInvalid)
}

func endBackgroundTask() {
    print("Background task ended.")
    AppConstants.sharedInstance.scheduleLocalNotification(message: "Background task ended.", title: "status!")
    UIApplication.shared.endBackgroundTask(backgroundTask)
    backgroundTask = UIBackgroundTaskInvalid
}

self.registerBackgroundTask()
self.perform(#selector(self.backgroundTimer(challengeId:)), with: "\(tag)", afterDelay:600)

【问题讨论】:

    标签: ios swift uiapplication performselector


    【解决方案1】:

    正如在doc 中发现的,只有预定义的(由 Apple)任务(在doc 的表 3-1 中)被允许无限期地运行。在任务完成之前,您无法执行任务。但是,如果您的目标是越狱设备,您可以通过编写守护程序来实现。这对你来说似乎不是这样。所以,你没有运气。

    【讨论】:

      猜你喜欢
      • 2020-11-21
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 2021-10-19
      • 1970-01-01
      相关资源
      最近更新 更多