将数据从CSV保存到Realm

时间:2016-08-15 11:14:21

标签: java android csv realm

我一直在创建一个词典应用程序,我将所有单词保存在 .csv 文件中 当应用程序首次运行时,此方法应将所有单词保存到领域数据库,但只保存少量。

  

任务java.util.concurrent.FutureTask@286858df拒绝了io.realm.internal.async.RealmThreadPoolExecutor@273caff5 [正在运行,池大小= 13,活动线程= 13,排队任务= 100,已完成任务= 1] < / p>

realm = Realm.getDefaultInstance();



    //adding to db satrt
    if (!(realm.isEmpty())) {
        Log.v("DB","already there!!");
    } else {
        Log.v("DB","Not Found!!");
        String csvFile = "longevity.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            br = new BufferedReader(new InputStreamReader(getAssets().open(csvFile)));
            while ((line = br.readLine()) != null) {

                // use comma as separator
                final String[] oneWord= line.split(cvsSplitBy);



                realm.executeTransactionAsync(new Realm.Transaction() {
                    @Override
                    public void execute(Realm bgRealm) {
                        Word user = bgRealm.createObject(Word.class);
                        user.setWord(oneWord[1]);
                        user.setMeaning(oneWord[2]);
                        user.setSynonyms(oneWord[3]);
                    }
                }, new Realm.Transaction.OnSuccess() {
                    @Override
                    public void onSuccess() {
                        Log.v("TAGGED","SAVED");
                    }
                }, new Realm.Transaction.OnError() {
                    @Override
                    public void onError(Throwable error) {
                        Log.v("TAGGED","FAILED");
                    }
                });
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

有没有更好的方法来运送预装的Relam dataBase与android, 当应用程序运行时,我有146800个单词要保存到数据库第一次,这个方法只保存了几个单词,然后给出错误显示在上面!

1 个答案:

答案 0 :(得分:4)

使用1个交易而不是146800个交易。还可以考虑使用1个实例来保存Realm对象,而不是创建146800个对象。

    realm.executeTransactionAsync(new Realm.Transaction() {
        @Override
        public void execute(Realm bgRealm) {
            String csvFile = "longevity.csv";
            BufferedReader br = null;
            String line = "";
            String cvsSplitBy = ",";
            try {
                br = new BufferedReader(new InputStreamReader(getAssets().open(csvFile)));
                Word user = new Word();
                while((line = br.readLine()) != null) {
                    // use comma as separator
                    final String[] oneWord = line.split(cvsSplitBy);
                    user.setWord(oneWord[1]);
                    user.setMeaning(oneWord[2]);
                    user.setSynonyms(oneWord[3]);
                    bgRealm.insert(user);
                }
            } catch(Throwable e) {
                e.printStackTrace();
                throw e;
            } finally {
                if(br != null) {
                    try {
                        br.close();
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            Log.v("TAGGED", "SAVED");
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            Log.v("TAGGED", "FAILED");
        }
    });

编辑:你应该考虑使用CSV parser library,因为String.split()是内存密集型的。

而不是

while ((line = br.readLine()) != null) {

考虑一下

final CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
for (final CSVRecord record : parser) {
    ...