领域java - RealmResults或RealmQuery上的批量更新

时间:2016-11-02 14:44:36

标签: java realm

在Realm ObjC中,可以对RLMResults集合执行更新操作,如下所示:

RLMResults<Pubs *> *pubsToDelete = [Pubs objectsWhere: @"NOT (pubId IN %@)", apiIds];

[[RLMRealm defaultRealm] transactionWithBlock:^{
   // all pubs in pubsToDelete will be updated 
   [pubsToDelete setValue:@YES forKeyPath:@"deleted"];
}];

是否也可以在Java版本中或我必须对RealmResults执行迭代并为每个对象设置值?像

RealmResults<Pubs> results = query.findAll();

realm.beginTransaction();
for (Pubs p : results) {
    p.deleted = 1;
}
realm.commitTransaction();

1 个答案:

答案 0 :(得分:3)

是的,在Realm-Java中,您需要迭代所有对象。

realm.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
        RealmResults<Pubs> results = query.findAll();
        for(Pub p : results) {
            p.deleted = 1;
        }
    }
});