MatterGoal
MatterGoal

Reputation: 16430

ApplicationContext property always return an Empty dictionary

I'm trying to get the latest data from a WCSession and I can't understand why even if I've just received the didReceiveApplicationContext call. More details are available directly into the code:

//Watch Code

override func awakeWithContext(context: AnyObject?) {
    super.awakeWithContext(context)
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session?.delegate = self
        session?.activateSession()
        verifyUser()
    }
}

// 1. This function is called, with the applicationContext data 
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
    print("CONTEXT \(applicationContext)")
}

// 2. I manually trigger this call from the watch with a button.
// even if I call this function after the previous function (1) it always print an Empty ([:]) applicationContext. 
@IBAction func printContext(){
    print(session?.applicationContext)
}

I expect the applicationContext property to be always updated with the latest information set using the updateApplicationContext since I always use the same WCSession obtained with WCSession.defaultSession for both the iOS and Watch app. Is there anything that I'm misinterpreting about connectivity?!

Upvotes: 4

Views: 745

Answers (2)

aarons22
aarons22

Reputation: 147

At least on WatchOS 6, receivedApplicationContext is empty until the session has been activated, which means if you are launch the app you'll need to wait until func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) is called and then validate the state is active before you can get the latest context.

Hoping this helps someone who landed here and faced the same issue I did 😄

Upvotes: 4

ccjensen
ccjensen

Reputation: 4656

receivedApplicationContext is what you are looking for. applicationContext's contents is the stuff you've sent out, not received.

Upvotes: 9

Related Questions