一段时间后,NSTimer在后台停止

时间:2022-09-13 01:24:50

I used NSTimer to call a method after 1 sec when app goes in background and after nearly 17 minutes timer stops working.

当应用程序进入后台时,我使用NSTimer在1秒后调用方法,并且在将近17分钟后计时器停止工作。

When it came back in the foregound it again started working, so please tell me why this is happening and how to resolve the issue.

当它回到正轨时它再次开始工作,所以请告诉我为什么会发生这种情况以及如何解决这个问题。

I also tried using perform selector with delay in recursion for the same purpose, but again giving the same result. Please suggest any solution. Any help will be completely appreciated.

为了相同的目的,我也尝试使用递归延迟的执行选择器,但同样给出相同的结果。请建议任何解决方案。任何帮助将完全赞赏。

3 个解决方案

#1


6  

Wierd issue- i too had this one, but the following worked for me.

奇怪的问题 - 我也有这个问题,但以下对我有用。

 //Run the timer on the runloop to ensure that it works when app is in background

  [[NSRunLoop currentRunLoop]addTimer:self.timer forMode: NSDefaultRunLoopMode];

#2


5  

From Implementing Long-Running Background Tasks:

从实现长期运行的后台任务:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

对于需要更多执行时间来实现的任务,您必须请求特定权限才能在后台运行它们而不会被挂起。在iOS中,只允许在后台运行特定的应用类型:

  • Apps that play audible content to the user while in the background, such as a music player app
  • 在后台播放用户可听内容的应用,例如音乐播放器应用

  • Apps that keep users informed of their location at all times, such as a navigation app
  • 可让用户随时了解其位置的应用,例如导航应用

  • Apps that support Voice over Internet Protocol (VoIP)
  • 支持互联网协议语音(VoIP)的应用

  • Newsstand apps that need to download and process new content
  • 需要下载和处理新内容的报亭应用程序

  • Apps that receive regular updates from external accessories
  • 从外部配件接收定期更新的应用程序

#3


0  

If few minutes isn't enough, you can't make an app run infinite time in backgorund unless it uses:

如果几分钟是不够的,你不能让应用程序在backgorund运行无限时间,除非它使用:

  • Audio Playback
  • Location Services
  • External Accessory

You can use Background Tasks to get 10 minutes of running time for all other apps, or Local Notifications to notify user.

您可以使用后台任务为所有其他应用程序或本地通知获取10分钟的运行时间以通知用户。

#1


6  

Wierd issue- i too had this one, but the following worked for me.

奇怪的问题 - 我也有这个问题,但以下对我有用。

 //Run the timer on the runloop to ensure that it works when app is in background

  [[NSRunLoop currentRunLoop]addTimer:self.timer forMode: NSDefaultRunLoopMode];

#2


5  

From Implementing Long-Running Background Tasks:

从实现长期运行的后台任务:

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

对于需要更多执行时间来实现的任务,您必须请求特定权限才能在后台运行它们而不会被挂起。在iOS中,只允许在后台运行特定的应用类型:

  • Apps that play audible content to the user while in the background, such as a music player app
  • 在后台播放用户可听内容的应用,例如音乐播放器应用

  • Apps that keep users informed of their location at all times, such as a navigation app
  • 可让用户随时了解其位置的应用,例如导航应用

  • Apps that support Voice over Internet Protocol (VoIP)
  • 支持互联网协议语音(VoIP)的应用

  • Newsstand apps that need to download and process new content
  • 需要下载和处理新内容的报亭应用程序

  • Apps that receive regular updates from external accessories
  • 从外部配件接收定期更新的应用程序

#3


0  

If few minutes isn't enough, you can't make an app run infinite time in backgorund unless it uses:

如果几分钟是不够的,你不能让应用程序在backgorund运行无限时间,除非它使用:

  • Audio Playback
  • Location Services
  • External Accessory

You can use Background Tasks to get 10 minutes of running time for all other apps, or Local Notifications to notify user.

您可以使用后台任务为所有其他应用程序或本地通知获取10分钟的运行时间以通知用户。