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

executeTransactionAsync using handover

Open cmelchior opened this issue 10 years ago • 6 comments

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?

cmelchior avatar Jan 28 '16 19:01 cmelchior

:+1:

dalinaum avatar Jan 28 '16 20:01 dalinaum

see Cocoa realm/realm-cocoa#3136

nhachicha avatar Mar 16 '16 18:03 nhachicha

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?

saket avatar Mar 17 '16 09:03 saket

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

cmelchior avatar Apr 05 '16 11:04 cmelchior

:+1:

stk1m1 avatar Apr 05 '16 12:04 stk1m1

See also #4059, which might impact the design of this

cmelchior avatar Jan 30 '17 11:01 cmelchior