Android将数据库与可穿戴设备同步-DataMapItems被覆盖 [英] Android synchronizing database with wearable - DataMapItems are overwritten

查看:112
本文介绍了Android将数据库与可穿戴设备同步-DataMapItems被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将手持设备上SQLite数据库中的数据同步到可穿戴设备。
因此,我在手持设备上将Data API与以下代码结合使用:

I try to synchronize data from a SQLite database on the handheld to a wearable. Therefore I use the Data API with the following code on the handheld:

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();

然后在可穿戴设备上,我有一个DataApi.DataListener来查找更改:

Then on the 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 rows of my database to the wearable and receive them there.

现在我读到不必将数据复制到可穿戴设备上的新SQLite数据库中,而是直接使用已经将数据存储在自己的DataItembuffer中数据库。
例如
如何使用Android Wear的DataItem

Android Wear DataMap条目保持有效状态多长时间

Now I read that it is not necessary to copy the data into a new SQLite database on the wearable but instead use directly the DataItembuffer which already has the data stored in an own database. e.g. How to use DataItem of Android Wear or Android Wear How Long do DataMap entries Stay Valid

但是当我以后想用一种方法检索以前同步的数据时,只能从之前发送的20行,最后一行。似乎覆盖了我的项目...但是我当然需要全部...

But when I want to retrieve the previously synchronized data in a method later on, I get only one item from the 20 rows sent before, the last one. It seems to overwrite my items... But I would need 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();
        }
    });*/
}

使用await()和ResultCallback之间显然没有区别,我总是只得到最后一个项目...

There is obviously no difference between using await() and the ResultCallback, I always get only the last item...

我在这里做什么错了?

更新:
我注意到如果使用不同的URI,它会有所作为。如果我使用

UPDATE: I noticed that it makes a difference if I use different URIs. If I use

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

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

然后我在getDataItems方法中至少获得了两个项目,但仍然是每个URI的最后一个项目。
根据文档,每个DataItem由一个通常由节点ID和路径组成的URI标识。 https://developers.google.com/android/ reference / com / google / android / gms / wearable / DataItem
由于使用数百或数千条路径似乎无效,因此问题在于DataItem是否甚至可以替换SQLite数据库。 。

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

还是有一种方法可以在一个URI下包含多个DataItem?

Or is there a way having multiple DataItems under one URI?

推荐答案

如果对多个数据项使用相同的路径,则每次使用该路径放置一个新路径时,都可以有效地更新数据,从而可以解释为什么仅获得最后一次更新;如果要访问并单独检索所有内容,则需要为每个路径使用不同的路径或使用不同的数据结构(例如数组)。无论如何,不​​建议使用DataApi提供的数据存储来代替sqlite数据库。 DataLayer存储在设计时考虑了一组不同的目标,因此,如果您真的想将数据存储在另一端以运行查询等,那么最好从Data存储中检索它们并将它们放入自己的存储中sqlite数据库可满足您的需要(并在处理它们时从数据存储中删除它们)。

If you use the same "path" for multiple data items, you are effectively updating the data each time you put a new one with that paths so that explains why you get only the "last" update; if you want to have access and retrieve all individually, you need to use different path or use a different data structure (say, an array) for each path. Regardless, using the data storage provided by the DataApi as a replacement for sqlite database is not recommended; the DataLayer storage is designed with a different set of goals in mind so if you really want to store the data on the other end for running queries against, etc, you are better off to retrieve them from the Data storage and put them into your own sqlite database for the purpose you have in mind (and delete the items from the Data storage as you process them).

这篇关于Android将数据库与可穿戴设备同步-DataMapItems被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆