如何让应用在后台运行更长时间

时间:2022-06-30 04:20:38

I have a Swift program that gets the user location based on certain conditions and save the location to a local database.

我有一个Swift程序,它根据特定条件获取用户位置,并将位置保存到本地数据库。

The application can also run on the background state. Since I need to get a very accurate location, it can take up to 60 seconds to get the location. If location is determine in less than 5 sec, everything works fine while app is running on the background. If it takes more than 5 secs to get the location, then nothing will happen. I think the reason is because you have only a few seconds to complete the background task.

应用程序还可以在后台状态下运行。因为我需要得到一个非常准确的位置,所以要花60秒才能得到这个位置。如果位置在5秒内确定,当app在后台运行时,一切正常。如果需要超过5秒才能找到位置,那么什么也不会发生。我认为原因是你只有几秒钟的时间来完成后台任务。

So is it possible to request up to 60 secs for the background task to run? Do I need to add any special code to tell the OS please allow more time (up to 60 secs) for the background task to complete?

因此,是否可能要求最多60秒的后台任务运行?我是否需要添加任何特殊的代码来告诉操作系统,请允许更多的时间(最多60秒)完成后台任务?

3 个解决方案

#1


1  

Yes.

是的。

Use the UIApplication function beginBackgroundTaskWithExpirationHandler:. In recent versions of iOS That gives you 3 minutes to finish whatever it is you're doing. See the Xcode docs for more information.

使用UIApplication函数beginBackgroundTaskWithExpirationHandler:。在iOS的最新版本中,你可以在三分钟内完成你正在做的事情。更多信息请参见Xcode文档。

#2


0  

You can use 'beginBackgroundTaskWithExpirationHandler'

您可以使用“beginBackgroundTaskWithExpirationHandler”

Sample code can be like this:

示例代码如下所示:

var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

    func registerBackgroundTask() {
  backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
    [unowned self] in
    self.endBackgroundTask()
  }
  assert(backgroundTask != UIBackgroundTaskInvalid)
}

func endBackgroundTask() {
  NSLog("Background task ended.")
  UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
  backgroundTask = UIBackgroundTaskInvalid
}

More information regarding this can be found in this tutorial about background modes: Raywenderlich:Background modes - Swift
Apple Doc: Background Modes

关于这方面的更多信息可以在本教程中找到:Raywenderlich:背景模式- Swift Apple Doc:背景模式

#3


0  

The best way to run your app in background and always on is to keep enable the background mode provided by xcode it self

在后台运行你的应用程序的最好方法是保持你的后台模式是由xcode自己提供的。

please refer : https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

请参阅:https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

Also set Bool "Application does not run in the background" in info.plist in your application

在info中还设置Bool“应用程序不在后台运行”。plist在您的应用程序

Hope this will help

希望这将帮助

Thank you

谢谢你!

#1


1  

Yes.

是的。

Use the UIApplication function beginBackgroundTaskWithExpirationHandler:. In recent versions of iOS That gives you 3 minutes to finish whatever it is you're doing. See the Xcode docs for more information.

使用UIApplication函数beginBackgroundTaskWithExpirationHandler:。在iOS的最新版本中,你可以在三分钟内完成你正在做的事情。更多信息请参见Xcode文档。

#2


0  

You can use 'beginBackgroundTaskWithExpirationHandler'

您可以使用“beginBackgroundTaskWithExpirationHandler”

Sample code can be like this:

示例代码如下所示:

var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid

    func registerBackgroundTask() {
  backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
    [unowned self] in
    self.endBackgroundTask()
  }
  assert(backgroundTask != UIBackgroundTaskInvalid)
}

func endBackgroundTask() {
  NSLog("Background task ended.")
  UIApplication.sharedApplication().endBackgroundTask(backgroundTask)
  backgroundTask = UIBackgroundTaskInvalid
}

More information regarding this can be found in this tutorial about background modes: Raywenderlich:Background modes - Swift
Apple Doc: Background Modes

关于这方面的更多信息可以在本教程中找到:Raywenderlich:背景模式- Swift Apple Doc:背景模式

#3


0  

The best way to run your app in background and always on is to keep enable the background mode provided by xcode it self

在后台运行你的应用程序的最好方法是保持你的后台模式是由xcode自己提供的。

please refer : https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

请参阅:https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial

Also set Bool "Application does not run in the background" in info.plist in your application

在info中还设置Bool“应用程序不在后台运行”。plist在您的应用程序

Hope this will help

希望这将帮助

Thank you

谢谢你!