flutter 后台任务
Hi everybody, I’m Riccardo. Senior iOS Engineer at Bending Spoons, I breathe iOS development, both apps and tools and I love to share my knowledge with others
大家好,我是Riccardo。 Bending Spoons的高级iOS工程师,我负责iOS开发,包括应用程序和工具,我喜欢与他人分享我的知识
Last week, a question was running through our (virtual) office: how much time does iOS leave to the app to complete a background task?
上周,我们(虚拟)办公室里出现了一个问题:iOS留给应用程序多少时间来完成后台任务?
As often happens in the technology world, the documentation is not that precise. Also, there is a lot of misinformation that makes it hard to find the right answer: someone says 30" others 3'. So… I decided to run a small experiment to find out and here it is what I did and what I discovered.
正如技术界经常发生的那样,文档并不是那么精确。 另外,还有很多错误信息,很难找到正确的答案:有人说30“别人3'。所以……我决定进行一个小实验,以找出答案,这就是我所做的事情和发现的事情。
技术 (Technology)
The technology chosen to run this test is the old UIApplication.beginBackgroundTask(withName:expirationHandler:)
. This method does two things:
选择运行该测试的技术是旧的UIApplication.beginBackgroundTask(withName:expirationHandler:)
。 此方法有两件事:
- It informs the OS that, once put into the background, the app needs more time to complete a long-running task. 它告知OS,将其置于后台后,该应用需要更多时间才能完成长时间运行的任务。
It returns an identifier that can be used to inform the OS that the task is completed (by invoking the
UIApplication.endBackgroundTask(_:)
method).它返回一个标识符,该标识符可用于通知OS任务已完成(通过调用
UIApplication.endBackgroundTask(_:)
方法)。
If we forget about calling the beginBackgroundTask
method, the OS will assign something like 5" to complete the ongoing tasks and that could not be enough.
如果我们忘记调用beginBackgroundTask
方法,则操作系统将分配5“之类的内容来完成正