Reputation: 12559
I have two language arrays in my arrays.xml. All languages and default languages.
I have a Language class which has two fields. Name and isSelected.
I am adding all languages to my Realm database on First run of my app. I am setting isSelected field of language if the language is in default languages array.
My question is when I update the all languages in my arrays.xml and publish an update for my app, how can I get the old realm data for selected languages and then clear the realm db and create a brand new with all languages in my arrays.xml and set the isSelected with the old realm data for selected languages?
Upvotes: 0
Views: 336
Reputation: 1285
realm.executeTransaction(realm1 -> realm1.copyToRealmOrUpdate(languageList));
realm.executeTransaction(realm1 -> realm1.delete(LanguageClass.class));
realm.executeTransaction(realm1 -> realm1.copyToRealmOrUpdate(languageList));
Realm.deleteRealm(realmConfiguration);
realm.executeTransaction(realm1 -> realm1.copyToRealmOrUpdate(languageList));
NOTE: The most recommended option in your case would option no.1, which is the most safe and reliable way to update data in Realm.
NOTE2: Instead of copying language data every time, you should only copy/update language data when there is a change in the language data.
Upvotes: 1