简体   繁体   English

提高在 Android 上将 JSON 流存储到本地数据库的性能

[英]Increase the performance of storing a JSON stream to a local database on Android

I am developing an application that (part of its functionality) is fetching a huge list of items from a server, in JSON format, and stores it in a local SQLite database.我正在开发一个应用程序(其部分功能)正在从服务器以 JSON 格式获取大量项目,并将其存储在本地 SQLite 数据库中。 Since the JSON file is huge, I am using the Gson library to perform the fetch, otherwise the application crashes because there is not enough RAM.由于 JSON 文件很大,我使用 Gson 库来执行 fetch,否则应用程序会因为 RAM 不足而崩溃。 Therefore, my only option is to fetch the file as a stream.因此,我唯一的选择是将文件作为流获取。

The JSON file structure is similar to this: JSON 文件结构类似于:

{
  "items": [
    {
        "itemID": "id1",
        "itemName": "name1"
    },
    {
        "itemID": "id2",
        "itemName": "name2"
    }
  ]
}

My Java code to process the stream looks like this:我处理流的 Java 代码如下所示:

JsonReader reader = new JsonReader(url_buffered_reader);
reader.beginObject();

String itemId = null;
String itemName = null;

while (reader.hasNext()) {
    if (reader.nextName().equals("items")) {
        reader.beginArray();
        while (reader.hasNext()) {
            reader.beginObject();
            while (reader.hasNext()) {
                String name = reader.nextName();
                switch (name) {
                    case "itemID":
                        itemId = reader.nextString().trim();
                        break;
                    case "itemName":
                        itemName = reader.nextString().trim();
                        break;
                    default:
                        reader.skipValue();
                        break;
                }
            }
            reader.endObject();

            storeItemData(itemId, itemName);
        }
        reader.endArray();
    }
}

The function storeItemData(String id, String name) looks like this (it is in a class that extends SQLiteOpenHelper ):函数storeItemData(String id, String name)看起来像这样(它在一个扩展SQLiteOpenHelper的类中):

public void storeItemData(String id, String name) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("itemID", id);
    values.put("itemName", name);

    long newRowId = db.insertWithOnConflict(
            "items",
            null,
            values, SQLiteDatabase.CONFLICT_REPLACE);
}

The code is working fine.代码工作正常。 The data is successfully downloaded and stored.数据已成功下载并存储。 The problem is that the performance is not good.问题是性能不好。 Is there a more efficient way to store the data, so that the performance is improved?有没有更有效的方式来存储数据,从而提高性能?

There is overhead associated with inserting a record into a database.将记录插入数据库会产生相关的开销。 Assuming the database is your bottleneck, you should be able to increase write performance by wrapping your inserts into a transaction.假设数据库是您的瓶颈,您应该能够通过将插入内容包装到事务中来提高写入性能。

  1. Move SQLiteDatabase db = this.getWritableDatabase();移动SQLiteDatabase db = this.getWritableDatabase(); out of storeItemData .超出storeItemData
  2. Call db.beginTransaction();调用db.beginTransaction(); before you begin inserting.在开始插入之前。
  3. Call db.setTransactionSuccessful();调用db.setTransactionSuccessful(); and then db.endTransaction();然后db.endTransaction(); after you are done inserting.插入完成后。
  4. Be sure to catch any exceptions so you can gracefully handle any error that may result from an insert.确保捕获任何异常,以便您可以优雅地处理可能由插入导致的任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM