简体   繁体   中英

iOS Background task will finish while in foreground?

If I use this method to run a task, but keep app in foreground, will expirationHandler be called while app is in foreground?

I use this for starting location service everytime I enter background, but sometimes user enters background and immediately returns to app, will this call expirationHandler?

func beginBackgroundTask(expirationHandler handler: (() -> Void)? = nil) -> UIBackgroundTaskIdentifier

Documentation says:

A handler to be called shortly before the app's remaining background time reaches 0. Use this handler to clean up and mark the end of the background task. Failure to end the task explicitly will result in the termination of the app. The system calls the handler synchronously on the main thread, blocking the app's suspension momentarily.

https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio

It seems that expiration handler is not called in the foreground mode. After very long waiting I went into background mode and then after ~170 seconds expiration handlers were called for all reported previously background tasks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM