watchOS - 显示有关并发症的实时出发数据 [英] watchOS - Show realtime departure data on complication

查看:31
本文介绍了watchOS - 显示有关并发症的实时出发数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共交通应用,其中包含火车的实时出发数据.我想添加一个显示下一趟列车发车时间的复杂功能.

是否可以显示(或刷新)复杂功能的实时数据?例如,显示3 分钟到 X 站".根据来自公共交通 API 的信息,数据可能每分钟都在变化.

我应该如何在 watchOS 2 或 watchOS 3 上完成此操作?

我知道 ETA 应用程序会以复杂的方式显示旅行时间,但我不确定它们是如何实现的.

解决方案

是否可以实时更新?

  • 复杂功能并非旨在显示实时数据.频繁更新会影响能源效率并影响电池(在手表和手机上).

    <块引用>

    为了最大限度地减少功耗,ClockKit 要求您提供尽可能多的可用数据,然后缓存数据并在需要时呈现它.

  • 虽然没有固定次数的复杂时间线可以重新加载,但复杂数据源受每日执行时间预算的约束.

    <块引用>

    如果您应用的数据频繁更改,则可能难以提供足够的数据以在复杂情况下显示.更糟糕的是,如果您过于频繁地刷新复杂功能数据,您可能会超出执行时间预算,并且您的复杂功能可能要到第二天才会更新.

  • 每日预算用完后,调用 reloadTimeline(和 extendTimeline) 什么都不做.

    <块引用>

    如果您的复杂功能已超过其分配的每日执行时间预算,则调用此方法将无济于事.谨慎调用此方法.

复杂功能如何显示相对时间?

  • 您可以使用 CLKRelativeDateTextProvider 来创建一个可以按分钟变化的格式化相对时间.

    <块引用>

    一个 CLKRelativeDateTextProvider 对象创建一个格式化的字符串,用于传达当前日期和您指定的日期之间的时间差.您可以使用相对日期文本提供程序以有效的方式实现计时器或其他相对时间值.不要使用多个时间线条目来复制倒数计时器,而是使用相对日期文本提供程序创建单个时间线条目.当用户查看钟面时,ClockKit 会自动更新复杂功能中的相对时间值,提供最新的时间信息.

如何频繁更新复杂功能?

  • 您可以使用复杂功能推送更新(从远程服务器,或从 iOS 10 中的手机本地).

    每天最多只能进行 50 次复杂功能推送更新.

  • 您可以在手机上获取数据并使用 transferCurrentComplicationUserInfo.

    在 watchOS 2 中,这仅受每日预算的限制.在 watchOS 3 中,这现在限制为每天 50 次传输.

    参见 transferCurrentComplicationUserInfo 是否更适合复杂功能更新? 了解更多详情.

  • 在 watchOS 2 中,您可以使用 getNextRequestedUpdateDate 安排下次更新您的复杂功能.

    这种情况不能超过每十分钟发生一次.

    请注意,watchOS 3 应用应升级为使用后台刷新应用任务.主要好处是后台任务能够做的不仅仅是更新您的复杂功能.他们还可以处理获取数据、在数据到达后更新您的模型以及更新您的停靠点快照.​​

  • 最后,您可以安排手动更新.在 watchOS 3 中,推荐的方法是通过后台刷新应用任务.

    任务预算允许每小时 4 个任务.请参阅 scheduleBackgroundRefresh 了解更多详情.>

    请注意,后台刷新应用任务使用的 CPU 不得超过 10%.

推荐的 WWDC 2016 会议

正如在会谈中提到的,您应该在需要更新的时间安排更新.

对于您的用例,示例仅适用于公共交通运行时,并且仅适用于定期安排的出发时间会受到延误影响的情况.

苹果示例代码

Apple 提供了 WatchBackgroundRefresh 示例代码演示如何使用 WKRefreshBackgroundTask 在后台更新 WatchKit 应用程序.

要更新后台任务中的任何活动复杂功能,您只需添加代码来重新加载(或扩展)时间线:

让complicationServer = CLKComplicationServer.sharedInstance()用于 activeComplications {complicationServer.reloadTimelineForComplication(complication)}

I have an public transport app with realtime departure data for trains. I would like to add a complication that shows the departure time of the next train.

Is it possible to show (or refresh) realtime data on a complication? For example, showing "3 min. to station X." The data could change every minute, based on info that comes from the public transport API.

How should I accomplish this on watchOS 2 or watchOS 3?

I know the ETA app shows travel times in a complication, but I'm not sure how they achieve that.

解决方案

Are realtime updates possible?

  • Complications aren't designed to show realtime data. Frequent updates can affect energy efficiency and impact the battery (on both watch and phone).

    To minimize power usage, ClockKit asks you to provide as much data as you have available and then caches the data and renders it when needed.

  • While there is no fixed number of times a complication timeline can be reloaded, the complication data source is subject to a daily execution time budget.

    If your app’s data changes frequently, it might be difficult to provide enough data to display in a complication. Worse, if you refresh your complication data too frequently, you may exceed your execution time budget and your complication might not be updated until the following day.

  • Once the daily budget is exhausted, calls to reloadTimeline (and extendTimeline) do nothing.

    If your complication has already exceeded its allotted daily budget for execution time, calls to this method do nothing. Call this method sparingly.

How can a complication display relative times?

  • You can use a CLKRelativeDateTextProvider to create a formatted relative time that can change on a minute-by-minute basis.

    A CLKRelativeDateTextProvider object creates a formatted string that conveys the difference in time between the current date and a date that you specify. You use a relative date text provider to implement timers or other relative time values in an efficient way. Instead of using multiple timeline entries to replicate a countdown timer, create a single timeline entry with a relative date text provider. When the user views the clock face, ClockKit automatically updates the relative time value in your complication, providing up-to-date time information.

How could a complication be frequently updated?

  • You could use a complication push update (either from a remote server, or locally from the phone in iOS 10).

    There is a limit of 50 complication push updates per day.

  • You could fetch data on the phone and use transferCurrentComplicationUserInfo.

    In watchOS 2, this was only subject to the daily budget. In watchOS 3, this is now limited to 50 transfers per day.

    See Is transferCurrentComplicationUserInfo more suitable for complication update? for more details.

  • In watchOS 2, you could use getNextRequestedUpdateDate to schedule the next time to update your complication.

    This can't occur more often than every ten minutes.

    Note that watchOS 3 apps should be upgraded to use background refresh app tasks. The main benefit is that background tasks would be able to do more than merely update your complication. They can also handle fetching data, updating your model once the data arrives, as well as updating your dock snapshot.

  • Finally, you can schedule a manual update. In watchOS 3, the recommended way to do this would via a background refresh app task.

    The task budget permits 4 tasks per hour. See scheduleBackgroundRefresh for more details.

    Note that background refresh app tasks must not use more than 10% CPU.

Recommended WWDC 2016 sessions

As mentioned in the talks, you should schedule your updates around the times when they would be needed.

For your use case, examples would be only when public transit is running, and only when the regularly scheduled departure times would be affected by a delay.

Apple sample code

Apple provides WatchBackgroundRefresh sample code demonstrating how to use WKRefreshBackgroundTask to update WatchKit apps in the background.

To update any active complication within a background task, you would simply add code to reload (or extend) the timeline:

let complicationServer = CLKComplicationServer.sharedInstance()

for complication in activeComplications {
    complicationServer.reloadTimelineForComplication(complication)
} 

这篇关于watchOS - 显示有关并发症的实时出发数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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