当前位置:   article > 正文

flutter 后台任务_快速后台任务

flutter background task

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:) 。 此方法有两件事:

  1. It informs the OS that, once put into the background, the app needs more time to complete a long-running task.

    它告知OS,将其置于后台后,该应用需要更多时间才能完成长时间运行的任务。
  2. 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“之类的内容来完成正在进行的任务,而这还不够。

At this point, the documentation is quite shady: how much execution time do we have after that method invocation? Let’s find out!

在这一点上,文档还很阴暗:方法调用之后,我们需要多少执行时间? 让我们找出答案!

Disclaimer: Since iOS 13, Apple introduced a whole new way to handle background tasks: the BackgroundTaskScheduler. The documentation can be found here. However, this will limit the audience of your app to only those users who updated their phone to the newest iOS. In March 2020, Apple stated that 77% of iOS devices are now running iOS 13. If you are going to use only the newest technology for background tasks, 23% of iOS users won’t be able to use your app.

免责声明:自iOS 13开始,Apple引入了一种处理后台任务的全新方法: BackgroundTaskScheduler 。 该文档可在此处找到。 但是,这会将您的应用程序的访问者限制为仅将手机更新到最新iOS的那些用户。 2020年3月, Apple声明 77%的iOS设备现在正在运行iOS13。如果您仅将最新技术用于后台任务,则23%的iOS用户将无法使用您的应用程序。

本实验 (The experiment)

The experiment is pretty simple. The idea is to write in the UserDefaults:

实验非常简单。 这个想法是写在UserDefaults

  1. the timestamp when the app goes into the background

    应用程序进入后台的时间戳
  2. the timestamp when the expirationHandler is invoked

    调用expirationHandler时的时间戳

Then, we just have to compute the difference between these two timestamps. We will need to repeat the experiment several times to compute the average execution time and to give it some scientific relevance.

然后,我们只需要计算这两个时间戳之间的差即可。 我们将需要重复几次实验,以计算平均执行时间并赋予其一定的科学意义。

I decided to add a simple UI to see the results and a secondary estimation system, based on a timer. However, these were just additions to make the results more robust or interpretable and do not affects the core of the experiment.

我决定添加一个简单的UI来查看结果,并基于一个计时器来创建一个辅助估算系统。 但是,这些仅仅是为了使结果更可靠或更易于解释,并且不影响实验的核心。

代码 (The code)

In this section, I won’t report the code of the whole app but I will show only the relevant part for the experiment. Otherwise, the article will result in many lines of common, quite boring UI code.

在本部分中,我不会报告整个应用程序的代码,但仅显示实验的相关部分。 否则,本文将导致许多行常见的无聊UI代码。

Let’s start with the building blocks. All the code below will live in the AppDelegate. Remember that this is an experiment to get a specific piece of information, it won’t be an app and it won’t be maintained. That’s why I decided to go for a quick-and-dirty approach.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/308577
推荐阅读
相关标签