[英]Update realm table
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: Conversation.java里面有一个RealmList消息:
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);
如果是realm.copyToRealmOrUpdate(conversations);
怎么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. PS:请记住,我从数据库中删除了所有内容,因为我不希望我的数据库中的对话不再存在于服务器中。
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. 不要忘记您正在执行事务,因此如果copyToRealmOrUpdate()
失败,则事务将被取消 ,这意味着您不会丢失所有数据。
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. 就个人而言,我曾经采用“全部删除”方法,如果你可以清除所有表格,那么它不会导致问题,但如果你有第三个表格,你指的是Conversation
和Message
(例如User.class
),你将使所有关系无效。 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.