Batch or bulk updates
Feature request following this discussion: https://groups.google.com/forum/#!topic/realm-java/6NeywfWmCA4
Basically it allows setting a property on many objects in one operation just like this SQL:
UPDATE Foo SET isDeleted = 1 WHERE name = "123"
One option could be
RealmResults<Foo> foos = realm.where(Foo.class).equalsTo("name", "123");
realm.beginTransaction();
foos.updateAllObjects("isDeleted", 1);
realm.commitTransaction();
This should definitely be implemented mostly in C++ for better performance.
The priority will be low as there are clear and easy workarounds for this. This is mainly a performance optimization.
When it comes to code that makes changes in the database, the cost of the commit that will follow it will most likely dwarf all other contributions, so it might not make a significant difference in performance.
On Mon, Jan 19, 2015 at 9:41 AM, Brian Munkholm [email protected] wrote:
The priority will be low as there are clear and easy workarounds for this. This is mainly a performance optimization.
— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/762#issuecomment-70460794.
That all depends on the number of objects you want to update and if you persist to disk or possible in-memory. But commit to disk and updating few objects, you are right.
The cost of commit does grow with the amount that is committed, the break-even point may be further out than we'd like.
/Finn
On Mon, Jan 19, 2015 at 11:16 AM, Brian Munkholm [email protected] wrote:
That all depends on the number of objects you want to update and if you persist to disk or possible in-memory. But commit to disk and updating few objects, you are right.
— Reply to this email directly or view it on GitHub https://github.com/realm/realm-java/issues/762#issuecomment-70471435.
It would be great if a batch processing feature is implemented as this feature can add the IN clause.
+1 Will really make things look prettier.
What exactly is the workaround? Is it this:
RealmResults<Foo> results = realm.where(Foo.class).equalTo("read", false).findAll();
for (int i = 0; i < results.size(); i++) {
results.get(i).setRead(true);
}
I tried this and it doesn't update anything after 200 records for me, is there a limit or a soft fail if it reaches a memory threshold? I'm wrapping the above code in a realm.executeTransaction. A method for updateAllObjects makes total sense.
@basememara It's a wrong idea to loop over a RealmResults while updating it. Since you're changing the RealmObjects (Foo in your case), the results is getting auto-updated on each update. Your indices will never be consistent!
@Saketme It depends on where you begin your transaction. As setRead() requires a transaction, the code could be
realm.beginTransaction();
RealmResults<Foo> results = realm.where(Foo.class).equalTo("read", false).findAll();
for (int i = 0; i < results.size(); i++) {
results.get(i).setRead(true);
}
realm.commit();
No, sorry not being clear earlier. I am talking about something else. This line of code get all Foo items that are "unread", right?
realm.where(Foo.class).equalTo("read", false).findAll();
Now, if you iterate over it using a for-i loop, setting each item to "read", it will cause the RealmResults list to auto-update itself on every iteration.
On every iteration, on every item that gets marked as "read", the RealmResults list will contain one less item, because it's only supposed to contain "unread" items.
As a result, the for loop will skip every alternate item.
@Saketme Well, you can do
realm.beginTransaction();
RealmResults<Foo> results = realm.where(Foo.class).equalTo("read", false).findAll();
for (int i = results.size() - 1; i >= 0; i--) {
results.get(i).setRead(true);
}
realm.commit();
Aha! That's a really nice idea :D Thanks!
Seriously though, this is in iOS here:
https://realm.io/docs/swift/latest/#key-value-coding
Realm really will need something like this, because creating 3000 objects to update 3000 properties isn't very good for GC.
I tend to agree.
Having RealmCollection.setValue(String fieldName, Object value) would probably be relatively easy and straight forward. It would not support manipulating a RealmList but that is probably outside the scope of this issue anyway.
For reference: https://www.reddit.com/r/androiddev/comments/5npdk2/big_realmresult_how_to_iterate_through_all_of_it/, some users are hitting memory issues because of this, so we should probably prioritize getting it in relatively soon.
Any chance of this one appearing any time soon?
It's kinda a necessity for working with larger data sets.
@cmelchior Out of curiousity, then realmList.where().findAll().setValue(fieldName, value) wouldn't work either?
@cmelchior @Zhuinden Realm Core doesn't have a such a setter so if we were to implement setValue(), we have to loop. Of course we could move the looping to C++ to lower the memory pressure.
@kneth but iOS has this 😕
Applying KVC to a collection is a great way to update objects in bulk without the overhead of iterating over a collection while creating accessors for every item.
RLMResults<Person*> *persons = [Person allObjects];
[[RLMRealm defaultRealm] transactionWithBlock:^{
[[persons firstObject] setValue:@YES forKeyPath:@"isFirst"]; // <------
// set each person's planet property to "Earth"
[persons setValue:@"Earth" forKeyPath:@"planet"]; // <-------
}];
Creating a new object proxy for each updated element is bad for memory usage