Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call ExtensionDelegate to create/refresh data for Complication

All my data creation is done in the ExtensionDelegate.swift.

The problem is ExtensionDelegate.swift doesn't get called before the function getCurrentTimelineEntryForComplication in my ComplicationController.swift.

Any ideas? Here is my code and details:

So my array extEvnts is empty in my ComplicationController.swift:

    func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {

        let extEvnts = ExtensionDelegate.evnts
}

Because my ExtensionDelegate.swift hasn't gotten called yet, which is what creates the data for the array:

class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {

    private let session = WCSession.defaultSession()
    var receivedData = Array<Dictionary<String, String>>()
    static var evnts = [Evnt]()

    func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {

        if let tColorValue = userInfo["TeamColor"] as? String, let matchValue = userInfo["Matchup"] as? String {

            receivedData.append(["TeamColor" : tColorValue , "Matchup" : matchValue])
            ExtensionDelegate.evnts.append(Evnt(dataDictionary: ["TeamColor" : tColorValue , "Matchup" : matchValue]))

        } else {
            print("tColorValue and matchValue are not same as dictionary value")
        }

    }

    func applicationDidFinishLaunching() {
        // Perform any final initialization of your application.
        if WCSession.isSupported() {
            session.delegate = self
            session.activateSession()
        }
    }
}

EDIT:

Per Apple, it looks like this has something to do with it, but for some reason I have no idea how to actually implement it because I'm not able to call mydelegate.evnts:

// Get the complication data from the extension delegate.
let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
var data : Dictionary = myDelegate.myComplicationData[ComplicationCurrentEntry]!

So I've tried something like this, and still can't get it working because I'm still getting no data:

func someMethod() {
    let myDelegate = WKExtension.sharedExtension().delegate as! ExtensionDelegate
    let dict = ExtensionDelegate.evnts
    print("ExtensionDel.evnts: \(dict.count)")

}

like image 626
SRMR Avatar asked Feb 21 '16 22:02

SRMR


Video Answer


1 Answers

Useful question that helped me here

In the function requestedUpdateDidBegin() you can update the information that you will display in your complication. So in this method you may make a call to your parent app using a WatchConnectivity method like sendMessage:replyHandler:errorHandler: to receive new information.

You can use NSUserDefaults to store your imperative data that will be used in your ComplicationController, then load this information from NSUserDefaults for your complication. I store this data in user defaults so that I always have old data to display in case the new data fails to load.

like image 143
Antonioni Avatar answered Sep 21 '22 04:09

Antonioni