简体   繁体   中英

Saving Data from CSV to Realm

I have been creating a Dictionary App i have all words saved in the .csv file
when app is run First Time, this method should save all the words to realm db but saving only few.

Task java.util.concurrent.FutureTask@286858df rejected from io.realm.internal.async.RealmThreadPoolExecutor@273caff5[Running, pool size = 13, active threads = 13, queued tasks = 100, completed tasks = 1]

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

    }

is there A Better way to ship prepopulated Relam dataBase with android, i have 146800 words to be saved to db when app is run First Time, this method saving only few words then giving error show above!

Use 1 transaction instead of 146800 transactions. Also consider using 1 instance to save your Realm object, instead of creating 146800 objects.

    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");
        }
    });

EDIT: You should consider using a CSV parser library , because String.split() is memory-intensive.

Instead of

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

Consider this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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