Android - 将大JSON保存到Realm OutOfMemoryException

时间:2017-09-11 08:00:59

标签: android json out-of-memory realm

情况:

我的服务器上有一个很大的Json file (> = 50-100Mb),其中包含我在应用中需要的所有客户的信息。

我必须将文件下载到我的应用程序中,并将所有信息保存到我的Realm数据库中。 (此文件中 200k对象)。

Customer.class

这是保存在此数组中的对象:

public class Customer  extends RealmObject {
    public Customer(){}

    @PrimaryKey
    private String ID;
    private String FULL_NAME;
    private String PHONE_NO;
    private String CODICE_FISCALE;
    private Date MODIFIED_ON;

    //getters and setters

我的下载和保存代码:

这是AsyncTask,我删除了所有无用的代码以便于查看

    @Override
    protected Void doInBackground(String... params) {
        Realm realm = null;
        newSha = null;
        try {
            realm = Realm.getDefaultInstance();

            String url = "myurl";
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(120, TimeUnit.SECONDS)
                    .readTimeout(120, TimeUnit.SECONDS)
                    .build();

            Request request = new Request.Builder().url(url)
                    .addHeader("Content-Type", "application/json")
                    .build();

            Response response = client.newCall(request).execute();
            ResponseBody responseBody = response.body();

            try {
                String myHeader = response.header("Content-Disposition");
                if (myHeader != null) {
                    int position = myHeader.indexOf("filename=");
                    position += "filename=".length();
                    newSha = myHeader.substring(position);
                }
            } catch (Exception ex) {
                newSha = null;
            }

            if (responseBody == null) {
                return null;
            }
            InputStream is = responseBody.byteStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            final StringBuilder sb = new StringBuilder();

            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }

            response.close();
            responseBody.close();
            is.close();
            reader.close();

            //switch because i have 20-30 cases, this is the big file which gives problems
            switch (fileName) {
                case "Customers": {
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            try {
                                Log.wtf("SYNC", "Customer - start delete");
                                realm.delete(Customer.class);
                                Log.wtf("SYNC", "Customer - end delete, start create");  //this line is reached
                                realm.createAllFromJson(Customer.class, sb.toString());  
                                Log.wtf("SYNC", "Customer - end create");                //this line is never reached
                                setTitle(String.format("Completato! [%s]", title));

                                publishProgress(100, 1);
                                addSyncCompleted(false, null, null);
                                genericDbClass.setSHA(realm, new Customer(), newSha);
                            } catch (Exception error) {
                                newSha = null;                                            //this error is never thrown
                            }
                        }
                    });
                }
                break;

问题:

realm.createAllFromJson抛出一个OutOfMemoryException(没有真正抛出,应用程序刚刚关闭没有被解雇而且没有任何错误。我在日志中看到的只有像警告

这样的一些例外记录
W/art: Throwing OutOfMemoryError "Failed to allocate a 170348044 byte allocation with 16777216 free bytes and 162MB until OOM"

问题在于如何将这个大Json文件保存到Realm中。我也尝试过用Gson解析这个字符串但没有任何成功。该文件可能会及时增加大小,所以我该如何加快速度呢?

任何帮助都将受到赞赏,我已经坚持了一个星期。

1 个答案:

答案 0 :(得分:1)

Realm的流式变体createAllFromJson()直接接受InputStreamhttps://realm.io/docs/java/3.7.1/api/io/realm/Realm.html#createAllFromJson-java.lang.Class-java.io.InputStream-

这也可以防止内存不足。