swift - 如何停止后台更新任务?

标签 swift background dispatch

这是我在用户关闭应用程序时在后台执行的代码,但这是奇怪的行为,在执行 endBackgroundUpdateTask() 方法后,DispatchQueue 仍然没有停止...

我钢连续收到通知。

我做错了什么?

你可以尝试拿这段代码自己尝试一下,这真的很奇怪

var backgroundUpdateTask: UIBackgroundTaskIdentifier!

func beginBackgroundUpdateTask() {
    print("beginBackgroundUpdateTask")
    self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    print("endBackgroundUpdateTask")
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
}

func doBackgroundTask() {
    print("Strart")
    DispatchQueue.global().async {
        self.beginBackgroundUpdateTask()

        // Do something with the result.
        let timer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(AppDelegate.displayAlert), userInfo: nil, repeats: true)
        RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode)
        RunLoop.current.run()

        // End the background task.
        self.endBackgroundUpdateTask()
    }

    print("Finish")
}

func displayAlert() {
    print("displayAlert")
    let note = UILocalNotification()
    note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..."
    note.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(note)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    log.debug("applicationDidEnterBackground")
    self.doBackgroundTask()

}

编辑

var backgroundUpdateTask: UIBackgroundTaskIdentifier!
var timerr: Timer?

func beginBackgroundUpdateTask() {
        appDeligate.log.debug("beginBackgroundUpdateTask")
        self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        self.endBackgroundUpdateTask()
    })
}

func endBackgroundUpdateTask() {
    appDeligate.log.debug("endBackgroundUpdateTask")
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
    timerr = nil
}

func doBackgroundTask() {
    print("Strart")
    DispatchQueue.global().async {
        self.beginBackgroundUpdateTask()

        // Do something with the result.
        self.timerr = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(TestViewController.displayAlert), userInfo: nil, repeats: true)
        RunLoop.current.add(self.timerr!, forMode: RunLoopMode.defaultRunLoopMode)
        RunLoop.current.run()

        // End the background task.
        self.endBackgroundUpdateTask()
    }

    print("Finish")
}

func displayAlert() {
    print("displayAlert")
    let note = UILocalNotification()
    note.alertBody = "As a test I'm hoping this will run in the background every X number of seconds..."
    note.soundName = UILocalNotificationDefaultSoundName
    UIApplication.shared.scheduleLocalNotification(note)
}

最佳答案

真正的问题是计时器,我需要放置这一行

timerr?.invalidate()

这里

func endBackgroundUpdateTask() {
    appDeligate.log.debug("endBackgroundUpdateTask")
    UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
    self.backgroundUpdateTask = UIBackgroundTaskInvalid
    timerr?.invalidate()
}

但无论如何,这有点奇怪,因为我认为如果我停止 backgroundUpdate() ,里面的所有任务都必须自动失效和清除,但事实并非如此。

谢谢@matt

关于swift - 如何停止后台更新任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42050267/

相关文章:

julia - 在 Julia 中调用父仿函数

ios - 相机处于横向模式而不是全屏模式

objective-c - 从 NSString 中提取表情符号(unicode)

css - cookies 图案喜欢

html - Div应该填充高度

javascript - 了解 Dean Edwards 的附加事件 JavaScript

ios - 我的 TableViewController 卡住了(初学者)

swift - 在 UITableView 行中保存按钮的选择状态 (Swift)

Android Layer-List VectorDrawable大小

java - 多个参数的多重分派(dispatch)