androidandroid-wear-data-apiandroid-wear-notification

Wearable.DataApi.deleteDataItems is not working


I would appreciate any insight as tried everything already to make deleteDataItems working but it doesn't.

I've implemented a very simple scenario with creating new data items (ArrayList) in my main app and passing it over to the wearable client using Data API.

Here is few code snippets:

  public void SyncDataItemsMainApp()
  {

  if (mGoogleApiClient==null){mGoogleApiClient.connect();}

  ArrayList<String> myList;
  myList = new ArrayList<String>();

  myList.add(new String(strValue));   // I add different set of values each time so it could be anything. 

 //For simplicity lets assume I add "Books" and "Pens"


  PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/mylistitem");
  putDataMapReq.getDataMap().putLong(ACTION_TIME_STAMP, new Date().getTime());
  putDataMapReq.getDataMap().putStringArrayList("com.myapp.example",myList);

  PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
  Wearable.DataApi.deleteDataItems(mGoogleApiClient,putDataMapReq.getUri()); //I use this line to delete DataItems which were created in the past as I do not want to store all variables on the cloud but only the current ones in myList
  Wearable.DataApi.putDataItem(mGoogleApiClient, putDataReq);
  }

Here is wearable OnDataChanged"

  @Override
  public void onDataChanged(DataEventBuffer dataEvents) 
  {
    // TODO Auto-generated method stub
    for (DataEvent event : dataEvents)
    {
        // DataItem changed
        DataItem item = event.getDataItem();

        if (event.getType() == DataEvent.TYPE_CHANGED) 
        {

            if (item.getUri().getPath().compareTo("/mylistitem") == 0) 
            {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();


                ArrayList<String> myListItems =dataMap.getStringArrayList("com.myapp.example");
                populateWearableList(myListItems);
            }
        } 
    }   
}

Now the problem is that every time I parse an arrayList on wearable it always include all values that I've ever added to the Array in the main app, As I mentioned above, I only would like to store the last set of values included into the last call. Hence it doesn't seem that DeleteDataItems actually work at all. An interesting thing is that in Debug over bluetooth mode, I am able to see only last set of values, but once my app & wearable are connected to the cloud it creates additional instances of same values which is quite confusing.

Any help is highly appreciated as I am about to give with using DataAPI as tried everything already.


Solution

  • I found that the reason for this was DataApi Listener that hasn't been stopped while app was in the background mode. I had to place all Data Api calls into a separate WearableListenerService and everything started working just fine. I wish this would have been called out somewhere in the docs..