Search code examples
wear-osandroid-wear-data-api

In Android wear, is DataApi and MessageApi sequential?


For example, if I send:

  • MessageApi.send(1)
  • DataApi.putItem(2)
  • MessageApi.send(3)
  • DataApi.putItem(4)

Are we guaranteed that they come in at the correct order?


Solution

  • First, some clarifications are in order. Note that sendMessage() targets a single node while putDataItem() results in syncing data across the whole network. So to make the question more precise, I would assume you are talking about the situation where we are looking at a single node where the sendMessage() is targeting and at the same time, on the very same node we are looking at data changes. In addition, I am going to assume that the arrival at that node is measured when onMessageReceived() and onDataChanged() are called. With that, let's look at your question:

    1. putDataItem() deliveries and sendMessage() deliveries happen on two different channels so there is no guaranteed ordering between them.
    2. For messages delivered through sendMessage(), the order is guaranteed to be preserved, regardless of having a direct connection between the two end nodes or some hops in between.
    3. For putDataItem(), the order is preserved but you need to be aware that you may see fewer onDataChanged() invocations than putDataItem() when you are updating the same item; when an item is added and then updated and then updated again and again, some middle callbacks may be skipped but the final callback is guaranteed to reflect the net changes and for two different data items, the order is preserved.

    Hope this answers your question.