【问题标题】:Timer is not running more than 3 minutes in background定时器在后台运行不超过 3 分钟
【发布时间】:2018-03-10 12:12:08
【问题描述】:

我正在开发冥想应用程序。在这个应用程序中,我有一些音乐内容和一些使用计时器的无声冥想部分。定时器在前台工作正常,但在后台仅运行 3 分钟(当设备被锁定或用户按下主页按钮退出应用程序时)。我正在使用 swift4。我尝试过的:

var timer: Timer!
var timeCounter : Int!
var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
var backgroundTask = BackgroundTask()

@IBOutlet weak var timeLabel: UILabel!

 override func viewDidLoad() {
    super.viewDidLoad()

     self.backgroundTaskIdentifier = UIApplication.shared.beginBackgroundTask(expirationHandler: {
        UIApplication.shared.endBackgroundTask(self.backgroundTaskIdentifier!)
    })

    backgroundTask.startBackgroundTask()

    DispatchQueue.global(qos: .background).async {
    let currentRunLoop = RunLoop.current
        let timeInterval = 1.0
        self.timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.updateTimer), userInfo: nil, repeats: true)
        self.timer.tolerance = timeInterval * 0.1
        currentRunLoop.add(self.timer, forMode: .commonModes)
        currentRunLoop.run()
    }
  }
}

 @objc func updateTimer () { 
    timeCounter = timeCounter - 1
    let minutes = Int(timeCounter) / 60 % 60
    let seconds = Int(timeCounter) % 60

    print("timeCounter", timeCounter)
    if (timeCounter == 0){
        if self.timer != nil {
            self.timer?.invalidate()
            self.timer = nil
            player.play()
        }
    }

    timeLabel.fadeTransition(0.4)
    timeLabel.text = String(format: "%02i:%02i",minutes,seconds)
}

提前致谢。

【问题讨论】:

  • 这不是您想要的方法。计时器不在后台运行。 (而且你几乎不应该打电话给runloop.run();这是一件非常高级的事情。)当你完成后你没有正确地结束你的后台任务(你只在过期时才这样做,这不是一个好习惯。 ) 精确的解决方案将取决于您正在做什么。您可能想成为一个音乐应用程序并利用它来驱动其他一切。您可能需要考虑静音音频(这对电池有些不利,但可能是这种用户体验的理想选择)。您还可以查看 UserNotifications 来计时。
  • 鉴于您到目前为止所描述的,我认为您确实可以使用标准工具构建一些非常好的东西。 NSTimer 不是正确的,它完全取决于您想要的用户体验。
  • 感谢@RobNapier 的回复......我该如何解决这个问题?对于这种情况,您建议哪种最佳解决方案。所以当用户锁定设备屏幕时,我可以长时间播放计时器。
  • 解决这个问题很大程度上取决于你的用例;当您播放音频以及绝对必须运行代码时。您所描述的可以通过成为一个后台应用程序来完成,当您想要保持静音时播放静音音频(这将是最直接的)。见developer.apple.com/library/content/documentation/iPhone/…developer.apple.com/library/content/documentation/Audio/…
  • @RobNapier:Apple 不会批准使用此技巧的应用程序(如果他们发现):“在后台播放音频内容的应用程序必须播放有声内容而不是静音。”

标签: ios swift nstimer background-process


【解决方案1】:

通常,您从操作系统获得的在后台运行的时间有限。您可以使用以下命令检查后台剩余时间并做出反应:

UIApplication.shared.backgroundTimeRemaining

如果条件良好(设备已解锁、电池已充满...),您通常需要大约 160-180 秒。

您可以在Apples documentation 中找到详细信息。

当您想播放音频时,您可以使用“播放音频”后台模式以免被操作系统剪切:

根据您播放音频的方式,configuring the AudioSession 也可能会有所改善。


编辑: 我现在从您的评论中了解到,您希望您的应用每 4 分钟执行一次操作。我看到的唯一可能是使用BackgroundFetch 功能。但这并不能保证固定的时间间隔。

【讨论】:

  • 感谢回复。@Shallow....我已经这样做了,我的音乐播放器正在工作,但我想在后台运行计时器,让用户选择冥想时间。它可能是 1 小时或更长时间。所以我的问题是计时器在后台运行的时间不超过 3 分钟。我怎样才能运行它?
  • 查看我的更新答案。这是不可能的。您可以安排通知以提醒用户,但她必须单击通知才能运行应用程序。
猜你喜欢
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
  • 1970-01-01
  • 2017-10-08
  • 1970-01-01
相关资源
最近更新 更多