iOS操作扩展中的后台任务 [英] Background task in iOS action extension

查看:123
本文介绍了iOS操作扩展中的后台任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个应用程序操作扩展,该应用程序会为用户准备一些数据,然后使用MailCore2将此信息发送到SMTP服务器.准备数据的速度非常快,但是发送电子邮件可能会花费一些时间(取决于其大小).这就是为什么我正在寻找一种在后台处理发送活动的方法.但这会导致不同解决方案出现一些问题:

I'm working on an action extension for an app which prepares some data for the user and then uses MailCore2 to send this information to a SMTP server. Preparing data is done very fast, but sending the email might take some time (depending on its size). That's why I'm looking for a way to handle the sending activities in the background. But this results in some problems with different solutions:

  1. 使用 URLSession .这是在iOS扩展程序中处理大量上载或下载内容的第一种方法.但是问题在于MailCore2不使用URLSessions将数据传输到邮件服务器.因此,这是不可用的.还是有可能以某种方式在URLSession中包装"此调用?

  1. Using URLSession. This is the first way to go for handling big uploads or downloads in an iOS extension. But the problem is that MailCore2 does not use URLSessions to transmit the data to the mail server. So this is not usable. Or is it possible to somehow 'wrap' this call in an URLSession?

使用 UNUserNotificationCenter ,并在数据准备好后从扩展名发送本地通知.想法是在收到此通知后在主应用程序中启动发送任务.问题:仅当用户单击通知时才调用通知userNotificationCenter didReceive方法.一个简单的徽章通知不会调用委托方法.

Using UNUserNotificationCenter and send a local notification from the extension after data preparation. The idea is to start the sending task in the main app after receiving this notification. Problem: the notifications userNotificationCenter didReceive method is only called, when the user clicks the notification. A simple badge notification does not call the delegate method.

使用背景抓取.这在模拟器中可以正常工作,但是在iOS设备上,连接到SMTP服务器时出现错误.我遇到了 github.com/MailCore/mailcore2/issues/252.对于此问题,解决方案应为MCOSMTPSession.isUseHeloIPEnabled = true,但这可能不适用于所有服务器.所以也不是完美的解决方案.

Using Background Fetch. This is working fine in the simulator, but on an iOS device there is an error when connecting to the SMTP server. I came across the same HELO problem as described in github.com/MailCore/mailcore2/issues/252. For this issue, a solution should be MCOSMTPSession.isUseHeloIPEnabled = true, but this might not work for all servers. So also not a perfect solution.

任何想法,如何处理这样的任务?或如何处理上述解决方案之一?

Any ideas, how to handle such a task? Or how to deal with one of the solutions mentioned above?

,因为此问题尚未收到任何答案:尚不清楚或需要哪些其他信息?

as this question does not receive any answers yet: what is unclear or which additional information is required?

推荐答案

经过多次测试和失败之后,我发现以下解决方案可在扩展程序的后台执行长期执行的任务.即使扩展名已经完成,它也能按预期工作:

After many tests and failures, I found the following solution to execute a long performing task in the background of an extension. This works as expected, even if the extension is already finished:

func performTask()
{
    // Perform the task in background.
    let processinfo = ProcessInfo()
    processinfo.performExpiringActivity(withReason: "Long task") { (expired) in
        if (!expired) {
            // Run task synchronously.
            self.performLongTask()
        }
        else {
            // Cancel task.
            self.cancelLongTask()
        }
    }
}

此代码使用 ProcessInfo.performExpiringActivity() 在以下位置执行任务另一个线程.同步执行performLongTask()中的任务很重要.当到达块的末尾时,线程将终止并结束任务的执行.

This code uses ProcessInfo.performExpiringActivity() to execute the task in another thread. It’s important that the task in performLongTask() is executed synchronously. When the end of the block is reached, the thread will terminate and end the execution of your task.

主应用程序中也有类似的方法.在 iOS中的后台任务的小摘要中对此进行了详细说明.

A similar approach is also working in the main app. It's described in detail in a small summary for background tasks in iOS.

这篇关于iOS操作扩展中的后台任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆