Uploaded image for project: 'Realm Java SDK'
  1. Realm Java SDK
  2. RJAVA-795

Make it possible to pass objects to async transactions

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None

      Currently when you do an async transaction, you have to write a lot of boilerplate code to re-fetch the objects you want to use within the transaction. Since the transaction happens on a background thread, we cannot access the original thread-confined object, and even if we are dealing with frozen objects, we still have to get a live mutable version before we can update it.

      A common example:

      // get an identifier for the object we want to pass to the transaction
      final String idToChange = myCat.getId();
      
      // create an asynchronous transaction (it will happen on background thread)
      realm.executeTransactionAsync(bgRealm -> {
         // we need to find the Cat we want to modify from the background thread’s Realm
         Cat cat = bgRealm.where(Cat.class)
                          .equalTo(CatFields.ID, idToChange)
                          .findFirst();
      
         // do something with the cat
         cat.deleteFromRealm()
      });
      

      It would be much nicer, performant and less error prone if it was possible to pass objects along to the transaction, and it handled the handover internally:

      // no need to get an identifier
      // final String idToChange = myCat.getId();
      
      // create an asynchronous transaction (it will happen on background thread)
      realm.executeTransactionAsync(myCat, (bgRealm, cat) -> {
         // do something with the cat
         cat.deleteFromRealm()
      });
      

      Internally this could use our handover mechanics so it would work with both objects, lists, query results and frozen objects.

            Assignee:
            Unassigned Unassigned
            Reporter:
            alexander.stigsen@mongodb.com Alexander Stigsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: