在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();
答案 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;
}
}
});