realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Batch or bulk updates

Open cmelchior opened this issue 11 years ago • 39 comments

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();

cmelchior avatar Jan 19 '15 08:01 cmelchior

This should definitely be implemented mostly in C++ for better performance.

kneth avatar Jan 19 '15 08:01 kneth

The priority will be low as there are clear and easy workarounds for this. This is mainly a performance optimization.

bmunkholm avatar Jan 19 '15 08:01 bmunkholm

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.

finnschiermer avatar Jan 19 '15 10:01 finnschiermer

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.

bmunkholm avatar Jan 19 '15 10:01 bmunkholm

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.

finnschiermer avatar Jan 19 '15 11:01 finnschiermer

It would be great if a batch processing feature is implemented as this feature can add the IN clause.

njovy avatar Jan 21 '15 02:01 njovy

+1 Will really make things look prettier.

RobertoArtiles avatar Mar 26 '15 21:03 RobertoArtiles

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 avatar Jul 31 '15 04:07 basememara

@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!

saket avatar Nov 11 '15 10:11 saket

@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();

kneth avatar Nov 11 '15 11:11 kneth

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.

saket avatar Nov 12 '15 12:11 saket

@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();

kneth avatar Nov 12 '15 14:11 kneth

Aha! That's a really nice idea :D Thanks!

saket avatar Nov 12 '15 19:11 saket

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.

Zhuinden avatar Jan 06 '17 23:01 Zhuinden

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.

cmelchior avatar Jan 13 '17 12:01 cmelchior

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.

cmelchior avatar Jan 13 '17 12:01 cmelchior

Any chance of this one appearing any time soon?

It's kinda a necessity for working with larger data sets.

Zhuinden avatar Feb 24 '17 21:02 Zhuinden

@cmelchior Out of curiousity, then realmList.where().findAll().setValue(fieldName, value) wouldn't work either?

Zhuinden avatar Mar 18 '17 10:03 Zhuinden

@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 avatar Mar 20 '17 08:03 kneth

@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

Zhuinden avatar Mar 20 '17 09:03 Zhuinden