1

I am currently working on an watchOS app, for which I am trying to program a dynamic complication.

What I want to achieve is to display a changing value stored inside core data. So the complication value should update in respect to the value.

I have tried watching WWDC videos and so on, but everything seemed too advanced for this.

Here is a simplified form of my implementation:

func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
        
    let dataString = DataManager.shared.fetchData()
        
    let textProvider = CLKTextProvider(format: dataString)
    let template = CLKComplicationTemplateUtilitarianLargeFlat(textProvider: textProvider)
            
    handler(CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template))
}

Core Data Part:

func fetch() -> String {
    let request = Item.fetchRequest()
        
    do {
        let items = try persistenceController.moc.fetch(request)
        return ...
            
    } catch { ... }
}

func update() { //<--- Here I want to call getCurrentTimelineEntry and so update the complication
    ...
        
    persistenceController.saveContext()
        
    #if os(watchOS) //<--- What I have tried, not working...
    let server = CLKComplicationServer.sharedInstance()
    server.activeComplications?.forEach(server.reloadTimeline)
    #endif
}

Thank you!

Kai Zheng
  • 6,640
  • 7
  • 43
  • 66
  • This solved my problem. https://stackoverflow.com/a/32595164/8700044 Put this inside `update()`s `#if os(watchOS)` block. – Kai Zheng Sep 26 '20 at 11:34

0 Answers0