简体   繁体   中英

Save the information in a RealmList locally

I'm trying to create a list of objects using Realm.io. I've managed to create a RealmList of objects that extend Realm objects and convert it to a recycler view that displays all of the objects in the RealmList. I've also added the ability to add objects to the list.

However, whenever I close and re-open the app, all of the items that I added to the RealmList previously are gone and the list is started from scratch. How can I save a RealmList locally so that all of the items added to the list will remain when the app is closed and opened again?

If you modify your Realm objects definition and you have deleteRealmIfMigrationNeeded() in you Realm initialization all realm data is cleared.

If you don't have that option or you don't modify definitions data has to be the same between application restarts.

You should save the entities you put in the RealmList<T> into the Realm in a transaction, like so:

realm.executeTransaction(new Realm.Transaction() {
    realm.copyToRealmOrUpdate(realmList);
});

If you're not doing that, then you're just tinkering with your objects in memory in a fancy list, but you're never actually writing them to the Realm.

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