ios 后台任务_iOS后台任务

本文介绍了iOS设备上的后台任务处理,探讨了如何在应用程序进入后台时保持特定任务的运行,以确保用户体验的连贯性和应用程序的高效运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ios 后台任务

As an iOS Developer, i always find that background execution is a limitation we have learned to live with while designing and implementing apps. There are several use cases which can benefit with apps allowed to execute in background.

作为iOS开发人员,我总是发现后台执行是我们在设计和实现应用程序时学会的一个限制。 在允许在后台执行的应用程序中,有几种使用案例会受益。

With the introduction with iOS 13, Apple introduced the Background Tasks framework. This allows our apps to refresh content and perform small tasks while the app is in background.

随着iOS 13的引入,Apple引入了Background Tasks框架。 这允许我们的应用在后台运行时刷新内容并执行小任务。

Some important considerations while implementing Background Tasks are:

实施后台任务时,一些重要的注意事项是:

Power: The device battery level is a primary factor for backgroud tasks to be triggered. Also, while implementing our task, we should ensure that its informs the system of completion. This ensures that our task doesn’t drain power unnecessarily.

功率 :设备电池电量是触发后台任务的主要因素。 另外,在执行任务时,我们应确保其将完成情况告知系统。 这样可以确保我们的任务不会不必要地消耗电力。

Performance: Our task should be efficient and guarantee high performance. Avoid implementing unnecessary work in background and use GCD to

绩效:我们的任务应该高效并保证高性能。 避免在后台执行不必要的工作,并使用GCD

Let’s see how we can request background tasks execution for our apps:

让我们看看如何请求应用程序执行后台任务:

Step 1: Enable Background Processing capability for our app

步骤1:为我们的应用启用后台处理功能

Firstly, we need to enable Background Processing capability for our app. This is available under the Background Modes capability for the app

首先,我们需要为我们的应用启用后台处理功能。 在应用程序的“后台模式”功能下可用

Image for post
Step 2: Enable Background Processing in App Scheme
步骤2:在App Scheme中启用后台处理

Step 2: Add the Background Task Identifiers to info.plist

步骤2:将后台任务标识符添加到info.plist

We need to provide a unique identifier for our tasks and we have to specify the task identifiers in the info.plist under the Key Permitted background task scheduler identifiers

我们需要为任务提供唯一的标识符,并且必须在“ 允许的密钥” 后台任务计划程序标识符下的info.plist中指定任务标识符

Image for post
Step 2: Add task identifier to info.plist
步骤2:将任务标识符添加到info.plist

Step 3: Register the app for Background Processing

步骤3:注册应用以进行后台处理

We need to register our app for background tasks. There are two types of background tasks namely

我们需要为后台任务注册我们的应用程序。 有两种类型的后台任务,即

  • A BGProcessingTask is typically a time consuming task which is expected to take time to complete.

    一个 BGProcessingTask通常是一个耗时的任务,预计将需要一段时间才能完成。

  • A BGAppRefreshTask is typically a short task for performing actions that can be completed quickly.

    BGAppRefreshTask 通常是执行可以快速完成的动作的简短任务。

Image for post

To register our tasks, we need to specify the identifiers and a task block which will call a code that we want to execute in the background.

要注册我们的任务,我们需要指定标识符和一个任务块,该块将调用我们要在后台执行的代码。

The method handleAppRefresh(:) is called when the system runs a task with the specified identifier “com.amitthakur.sampleapp.refresh

当系统运行带有指定标识符“ com.amitthakur.sampleapp.refresh ”的任务时,将调用handleAppRefresh(:)方法

The method handleDataProcessing(:) is called when the system runs a task with the specified identifier “com.amitthakur.sampleapp.dataprocessing

当系统运行带有指定标识符“ com.amitthakur.sampleapp.dataprocessing ”的任务时,将调用handleDataProcessing(:)方法

Step 4: Submit task for execution

步骤4:提交执行任务

The next step is to submit() the task when the app goes to background. To do this, we fetch the request with the identifier specified.

下一步是当应用程序进入后台时,将任务提交() 。 为此,我们使用指定的标识符获取请求。

With a BGAppRefreshTask, we need to specify an earliestBeginDate which tells the system when to execute the task.

随着BGAppRefreshTask,我们需要指定一个earliestBeginDate它告诉系统何时执行任务。

Image for post
Submit BGAppRefreshTask
提交BGAppRefreshTask

With a BGProcessingTask, we should specify whether it requiresNetworkConnectivity and requiresExternalPower. These act as a pre-condition for our task execution and should be specified based on our task needs.

对于BGProcessingTask ,我们应该指定它是否需要 NetworkConnectivity和requireExternalPower 这些是我们执行任务的前提,应根据我们的任务需求进行指定。

Image for post
Submit BGProcessingTask
提交BGProcessingTask

Step 5: Implement Task handler

步骤5:实施任务处理程序

Now that we have registered our tasks, lets have a looks at how they work. Firstly, we reschedule our tasks so that we have another such task in the queue. This depends on whether your app expects periodic updates.

现在我们已经注册了任务,让我们看一下它们的工作方式。 首先,我们重新计划任务,以便队列中还有另一个此类任务。 这取决于您的应用是否期望定期更新。

Image for post
Handle BGAppRefreshTask with expirationHandler
使用expirationHandler处理BGAppRefreshTask
Image for post
Handle BGProcessingTask with expirationHandler
使用expirationHandler处理BGProcessingTask

With every task execution we must specify a few things:

在执行每个任务时,我们必须指定一些事项:

setTaskCompleted(success:) : Use this to mark the task execution as complete.

setTaskCompleted(success :)使用它可以将任务执行标记为完成。

expirationHandler: Use this to gracefully handle our task execution being terminated. Use this to perform cleanup or wrap up the task

expirationHandler: 使用它来优雅地处理我们终止的任务执行。 使用它执行清理或完成任务

Step 5: Schedule tasks when our app enters background

步骤5:在我们的应用进入后台时安排任务

Finally, don’t forget to schedule the tasks when our app enters background

最后,当我们的应用进入后台时,别忘了安排任务

Image for post

This should submit our tasks for execution in background.

这应该提交我们的任务以在后台执行。

In conclusion, having implemented background task execution in a few apps, it has definitely improved the app performance. However, triggering of the task is quite unpredictable. Apple has provided a way to invoke the task in debug mode, but more of that in the next blog on Background Tasks.

总而言之,在一些应用程序中实现了后台任务执行后,绝对可以提高应用程序的性能。 但是,任务的触发是非常不可预测的。 Apple提供了一种在调试模式下调用任务的方法,但在背景任务的下一个博客中提供了更多方法。

Hope this was helpful. Thanks for reading.

希望这会有所帮助。 谢谢阅读。

翻译自: https://medium.com/@i.m.amit.k.thakur/ios-backgroundtasks-435a8e58b9e0

ios 后台任务

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值