watchos

Is watchOS Application Context a two-way thing?


Our iPhone app has a Watch Extension, and it needs to send some data to the Watch as well as receive some data.

I'm confused on how this is supposed to work. If the iPhone sends an updateApplicationContext with data A, the Watch receives it, and then the Watch sends another updateApplicationContext with data B, does the call from the Watch override the previous call? Is A now replaced by B? The alternative would be that these are two channels, one channel for iPhone-to-Watch, and one for Watch-to-iPhone.

My tests seem to indicate that it's one channel. When calling updateApplicationContext on the Watch, it changes the Application Context on both devices, and it seems that the Watch receives a didReceiveApplicationContext event (even though it was the Watch that updated the data).

If this is true, then how are we supposed to avoid race conditions while updating the data? If the iPhone sends some data A, and the Watch wants to transfer some data B, how can we avoid data loss because the Watch overrides A by B? Can we separate the data by having two subnodes on the data, like:

context = {
  phone2Watch = {...},
  watch2Phone = {...},
}

I know I can use other means of data transfer, like UserInfo or sendMessage, but I'm specifically interested in the App Context mechanism.


Solution

  • I contacted Apple Developer Support on this issue, and they confirmed that App Context works this way. There is no way to use App Context to send data both ways and avoid race conditions.

    Seems we can use App Context one-way only, and everything else should go into UserInfo.