watchkitapple-watchwatchosapple-watch-complicationclockkit

watchOS - Show realtime departure data on 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.


Solution

  • Are realtime updates possible?

    How can a complication display relative times?

    How could a complication be frequently updated?

    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)
    }