Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add objects to Dictionary (instead of Dictionary only keeping most recent object) Swift

I'm passing (from iOS) some data (colors), but when I receive the data (colors) on the other side (WatchKit) it only shows the one most recent dictionary item.


iOS function to pass data:

publicData.performQuery(query, inZoneWithID: nil) { results, error in
            if error == nil { // There is no error
                for play in results! {
                    let newPlay = Play()
                    newPlay.tColor = play["TColor"] as! String

                    do {
                        try WatchSessionManager.sharedManager.updateApplicationContext(["color" : newPlay.tColor])
                        NSLog("Dict: %@", ["color" : newPlay.tColor])
                    } catch {
                        print(error)
                    }
                    self.objects.append(newPlay)
                }

All three color items show up in NSLog from function to pass data.

Dict: {
    color = FDB927;
}
Dict: {
    color = 000000;
}
Dict: {
    color = 000000;
}

WatchKit function to receive data:

func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
        NSLog("Application Context %@", applicationContext.description)
        dispatch_async(dispatch_get_main_queue()) { [weak self] in
            self?.dataSourceChangedDelegates.forEach { $0.dataSourceDidUpdate(DataSource(data: applicationContext))}
        }
    }

But on WatchKit side, only the last Dictionary item shows up in NSLog:

Application Context ["color": 000000, "matchup"]

Any help greatly appreciated. Thanks!

like image 641
SRMR Avatar asked May 13 '26 03:05

SRMR


2 Answers

This is exactly how the WatchConnectivity's updateApplicationContext works (only sends the most recent). If you want all the dicts to arrive you should instead use the transferUserInfo API, which queues up the dictionaries for delivery rather than just delivering the most recent one.

like image 130
ccjensen Avatar answered May 15 '26 17:05

ccjensen


Communicating with a Counterpart

You can communicate with a counterpart app in the following ways:

  • Use the updateApplicationContext:error: method to communicate recent state information to the counterpart. When the counterpart wakes, it can use this information to update its own state. Sending a new dictionary with this method overwrites the previous dictionary.
  • Use the sendMessage:replyHandler:errorHandler: or sendMessageData:replyHandler:errorHandler: method to transfer data to a reachable counterpart. These methods are intended for immediate communication between your iOS app and WatchKit extension. The reachable property must currently be YES for these methods to succeed.

You have to use sendMessage:replyHandler:errorHandler: instead of updateApplicationContext:error:

like image 31
Arsen Avatar answered May 15 '26 17:05

Arsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!