luky luky - 1 month ago 11
Java Question

realm java - bulk update on RealmResults or RealmQuery

In Realm ObjC it is possible to perform update operation on RLMResults collection like this:

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

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


Is it possible also in Java version or I have to perform iteration over RealmResults and set the value for each object? Like

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

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

Answer

Yes, in Realm-Java you need to iterate for all objects.

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