使用Swift 3和后台任务更新并发症 [英] Updating complication with Swift 3 and background task

查看:141
本文介绍了使用Swift 3和后台任务更新并发症的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于watchOS 3,Apple建议使用WKRefreshBackgroundTask而不是getNextRequestedUpdateDate来更新并发症.

For watchOS 3, Apple suggests updating the complication with WKRefreshBackgroundTask instead of using getNextRequestedUpdateDate.

如何使用新方法确定两次更新之间的时间?

How can I determine the time between two updates using the new approach?

我只会将我的数据请求(从url发送)到getCurrentTimelineEntry中,并会更新复杂性,但是我认为这并不是Apple真正推荐的.

I would only hack my data requesting (from an url) into getCurrentTimelineEntry and would update the complication, but I think that's not really what Apple would recommend.

一个简短的代码示例将有很大帮助.

A short code example would be a big help.

推荐答案

我通常以不同的答案涵盖了但我会在这里解决您的具体问题.

I generally covered this in a different answer, but I'll address your specific question here.

是正确的,您不应该破解复杂控制器来执行任何(异步)获取.数据源应仅 负责根据复杂服务器的要求返回现有数据.对于watchOS 2确实如此,在watchOS 3中仍然如此.

You're right that you shouldn't hack the complication controller to do any (asynchronous) fetching. The data source should only be responsible for returning existing data on hand as requested by the complication server. This was true for watchOS 2, and is still true in watchOS 3.

对于watchOS 3,每次后台刷新都可以安排下一次刷新.

For watchOS 3, each background refresh can schedule the next one.

在特定情况下,您可以等待直到WKURLSessionRefreshBackgroundTask任务完成下载.此时,请在完成您现有的后台任务之前,在 之前安排下一次后台刷新.

In your particular case, you can wait until your WKURLSessionRefreshBackgroundTask task finishes its downloading. At that point, schedule the next background refresh before completing your existing background task.

在将来的时间,您的扩展程序将再次被唤醒,以重新开始整个后台进程,以:

At that future time, your extension will be woken up again to start the entire background process again to:

  • 从您的网络服务中请求新数据
  • 处理回复并更新您的数据存储区
  • 告诉并发症要进行自我更新(它将使用现有的新数据).
  • 更新停靠快照
  • 安排即将进行的后台刷新任务
  • 将您当前的任务标记为已完成.

您甚至可以链接一系列不同的后台子任务,其中每个子任务分别处理刷新周期的一个方面,并负责安排以下子任务.

You can even chain a series of different background sub-tasks where each sub-task handles a separate aspect of a refresh cycle, and is responsible for scheduling the following sub-task.

如果您还没有看过,Apple会提供其 WatchBackgroundRefresh 示例代码来演示其中的一部分.您可以使用

If you haven't seen it, Apple provides its WatchBackgroundRefresh sample code to demonstrate part of this. You can use

WKExtension.shared().scheduleBackgroundRefresh(withPreferredDate:userInfo:)

在当前任务完成之前安排(初始任务或未来任务).

to schedule (either the initial, or) a future task before the present task completes.

尽管他们的示例使用刷新按钮安排下一次后台刷新,但是概念相同,无论是调度用户的下一个请求是用户操作还是后台任务.

Although their example uses a refresh button to schedule the next background refresh, the concept is the same, whether it is a user action or background task that schedules the next request).

这篇关于使用Swift 3和后台任务更新并发症的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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