executeTransactionAsync using handover
Using handover we can make async transactions much easier and nicer to use. It would also eliminate the need for primary keys on objects.
Foo obj = foo;
realm.executeTransactionAsync(foo, new Realm.Transaction() {
@Override
public void execute(Realm realm, Foo foo) {
foo.setX("X"); // foo has been handed over
}
});
// Also
realm.executeTransactionAsync(RealmResults results, Realm.Transaction transaction);
// And perhaps
realm.executeTransactionAsync(RealmObject[] objects, Realm.Transaction transaction);
Instead of
String key = foo.getKey();
realm.executeTransactionAsync(new Realm.Transaction.Callback() {
@Override
public void execute(Realm realm) {
Foo foo = realm.where(Foo.class).equalsTo("Key", key).findFirst();
foo.setX("X");
}
});
What do you think @realm/java?
:+1:
see Cocoa realm/realm-cocoa#3136
This would make asynchronous operations 10X easier!!
This workaround is exactly what I have to do in all my database methods. It's slightly frustrating and I ended up creating a helper class that captures the ID of the items and re-queries them when inside the async. transaction.
Please make this possible :pray:
Update: In some cases, I've to update multiple objects in a single async. transaction. Will handover be of any use in those situations?
Something like what is described above could do that:
realm.executeTransactionAsync(RealmObject[] objects, Realm.Transaction transaction);
// or
realm.executeTransactionAsync(Iterable<RealmObject> objects, Realm.Transaction transaction);
:+1:
See also #4059, which might impact the design of this