Android wearable database synchronization - DataMapItems overwritten

I am trying to synchronize data from a SQLite database on a PDA with a wearable. Therefore, I use the data API with the following PDA code:

cursor.moveToFirst(); while (!cursor.isAfterLast()) { final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE"); putRequest.setUrgent(); final DataMap map = putRequest.getDataMap(); int i = 0; for (String columnName: cursor.getColumnNames()) { map.putString(columnName,cursor.getString(i)); i++; } Wearable.DataApi.putDataItem(googleApiClient, putRequest .asPutDataRequest()).setResultCallback(new ResultCallback() { @Override public void onResult(Result result) { if (!result.getStatus().isSuccess()) { Log.v("MainActivity", "data could not be sent"); } else { Log.v("MainActivity", "data sent"); } } }); cursor.moveToNext(); } cursor.close(); 

Then on wearable I have a DataApi.DataListener to look for changes:

 @Override public void onDataChanged(DataEventBuffer dataEventBuffer) { for (DataEvent event : dataEventBuffer) { String data = null; try { data = new String(event.getDataItem().getData(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } Log.v(TAG, "onDataChanged data: "+ data); if (event.getType() == DataEvent.TYPE_CHANGED && event.getDataItem().getUri().getPath().equals("/SAMPLE")) { DataMapItem item = DataMapItem.fromDataItem(event.getDataItem()); String column1_value = item.getDataMap().getString(column1); Log.v(TAG, "onDataChanged column1: "+ column1_value); } } } 

This works well, I can send all the rows of my database to wearables and get them there.

Now I read that there is no need to copy data to a new SQLite database on wearable, but instead use DataItembuffer directly, which already has data stored in its own database. e.g. How to use DataItem for Android Wear or Android Wear How long DataMap entries remain valid

But when I want to get previously synchronized data in the method later, I get only one element from the 20 rows sent before the last one. I seem to be rewriting my stuff ... But to me, of course, all of them ...

 public void getDataItems(){ final PendingResult<DataItemBuffer> results = Wearable.DataApi.getDataItems(googleApiClient); Runnable r = new Runnable() { @Override public void run() { DataItemBuffer dataItems = results.await(); for (int i = 0; i < dataItems.getCount(); i++) { DataMapItem item = DataMapItem.fromDataItem(dataItems.get(i)); String column1_value = item.getDataMap().getString(column1); Log.v(TAG, "getDataItems column1: "+ column1_value); } } }; Thread thread = new Thread(r); thread.start(); /* results.setResultCallback(new ResultCallback<DataItemBuffer>() { @Override public void onResult(DataItemBuffer dataItems) { Log.v(TAG, "dataItems length: "+ dataItems.getCount()); for (int i = 0; i < dataItems.getCount(); i++) { DataMapItem item = DataMapItem.fromDataItem(dataItems.get(i)); String column1_value = item.getDataMap().getString(column1); Log.v(TAG, "getDataItems column1: "+ column1_value); } dataItems.release(); } });*/ } 

Obviously, there is no difference between using await () and ResultCallback, I always get only the last element ...

What am I doing wrong here?

UPDATE: I noticed that it matters if I use different URIs. If i use

 final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE1"); 

and

 final PutDataMapRequest putRequest = PutDataMapRequest.create("/SAMPLE2"); 

then I get at least two elements in the getDataItems method, but still only the last for each URI. According to the Docs, each DataItem is identified by one URI, usually consisting of a node identifier and a path. https://developers.google.com/android/reference/com/google/android/gms/wearable/DataItem Since it seems inefficient using hundreds or thousands of paths, the question arises whether DataItems can even replace SQLite db or not .. .

Or is there a way that has multiple data elements in one URI?

+2
source share
1 answer

If you use the same β€œpath” for several data items, you will effectively update the data each time you put a new one with these paths to explain why you are only getting the β€œlast” update; if you want to have access and get everything separately, you need to use a different path or use a different data structure (say, an array) for each path. Despite this, using the data store provided by DataApi as a replacement for the sqlite database is not recommended; The DataLayer repository is designed with a different set of goals in mind, so if you really want to save data on the other end to run queries, etc., you better extract them from the data repository and put them in your own sqlite database for the purposes you you mean (and delete items from the data warehouse when processing them).

+1
source

Source: https://habr.com/ru/post/1216495/


All Articles