I was just wondering if what I'm doing to update a entire table in realm is the correct safe approach. I receive a list of conversations from the server and update my db like this:
@Override
public void saveConversations(final List<Conversation> conversations) {
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
// Remove all conversations and replace with the one passed in
// Realm does not support cascade deletes. Remove messages as well
// https://github.com/realm/realm-java/issues/1104
realm.delete(Conversation.class);
realm.delete(Message.class);
realm.copyToRealmOrUpdate(conversations);
}
});
}
Conversation.java has a RealmList of messages inside:
public class Conversation extends RealmObject {
private RealmList<Message> messages = new RealmList<>();
This works, I couldn't find any bugs with it but it does not look particularly elegant. What if realm.copyToRealmOrUpdate(conversations);
goes wrong? I would lose all my data.
Anyways, I know this is not very probable, but I was wondering if there was a better way of doing things.
PS: bear in mind that I delete everything from the db because I don't want conversations in my db that don't exist in the server anymore.
Don't forget that you're executing a transaction, so if copyToRealmOrUpdate()
fails, then the transaction is cancelled , which means you wouldn't lose all your data.
Personally, I used to go with the "delete all" approach, and if you can clear out all tables then it won't cause issues, but if you have a third table where you're referring to Conversation
and Message
(for example User.class
), you'd be invalidating all relations. So I personally prefer to merge like this.
.
public class Contact {
@PrimaryKey
private long id;
@Index
private String name;
@Index
private String phoneNumber;
@Index
private boolean isBeingSaved; // this line is important
//getters, setters
}
Then merge:
// background thread
Realm realm = null;
try {
realm = Realm.getDefaultInstance();
final List<Contact> contacts = getContacts();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
realm.insertOrUpdate(contacts);
realm.where(Contact.class)
.equalTo(ContactFields.IS_BEING_SAVED, false) // compile 'dk.ilios:realmfieldnameshelper:1.1.0'
.findAll()
.deleteAllFromRealm(); // delete all non-saved data
// in your case, this is where you'd handle the cascading too though manually
for(Contact realmContact : realm.where(Contact.class).findAll()) { // realm 0.89.0+
realmContact.setIsBeingSaved(false); // reset all save state
}
}
});
} finally {
if(realm != null) {
realm.close();
}
}
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.