Reputation: 715
I need to store data that's controlled by the main Watch app (and the iPhone app) and displayed in a complication.
The official Apple documentation says
If you need to fetch or compute the data for your complication, do it in your iOS app or in other parts of your WatchKit extension (for example, by scheduling a background app refresh task), and cache the data in a place where your complication data source can access it.
What do they have in mind when they tell you to cache the data in a place where the complication can access it? What is the best practice/standard way to achieve this?
Upvotes: 3
Views: 479
Reputation: 5490
You could store some data in UserDefaults, and access that from your complication data source.
ie.
//In a background task
func getComplicationData(){
let yourData = someNetworkCall()
/*
yourData = [
"complicationHeader": "Some string",
"complicationInner": "Some other stirng"
]
*/
UserDefaults.standard.set(yourData, forKey: "complicationData")
}
Then in your ComplicationDataSource
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) {
if let yourData = UserDefaults.standard.dictionary(forKey: "complicationData") as? [String: String] {
//Handle setting up templates for complications
}
}
Upvotes: 0