Realm-java: Synchronous executeTransaction() with callbacks

Created on 22 Jun 2016  路  5Comments  路  Source: realm/realm-java

Feature Request: Just fill in the first two sections below.

Goal

Receive a callback on success/error when a _synchronous_ transaction is successfully completed by Realm

Expected Results

Success/error is called

Actual Results

Since

RealmObject and RealmResults are always updated on the next event loop.

you would need to receive a looper event to know that your transaction has successfully been completed and the results are up to date. However, the API does not allow synchronous transactions with callbacks.

void    executeTransaction(Realm.Transaction transaction)
Executes a given transaction on the Realm.

RealmAsyncTask  executeTransactionAsync(Realm.Transaction transaction)
Similar to executeTransaction(Transaction) but runs asynchronously on a worker thread.

RealmAsyncTask  executeTransactionAsync(Realm.Transaction transaction, Realm.Transaction.OnError onError)

Similar to executeTransactionAsync(Transaction), but also accepts an OnError callback.
RealmAsyncTask  executeTransactionAsync(Realm.Transaction transaction, Realm.Transaction.OnSuccess onSuccess)
Similar to executeTransactionAsync(Transaction), but also accepts an OnSuccess callback.

RealmAsyncTask  executeTransactionAsync(Realm.Transaction transaction, Realm.Transaction.OnSuccess onSuccess, Realm.Transaction.OnError onError)
Similar to executeTransactionAsync(Transaction), but also accepts an OnSuccess and OnError callbacks.

Which means there is no such thing as

    void    executeTransaction(Realm.Transaction transaction, Realm.Transaction.OnSuccess onSuccess)
    void    executeTransaction(Realm.Transaction transaction, Realm.Transaction.OnError onError)
    void    executeTransaction(Realm.Transaction transaction, Realm.Transaction.OnSuccess onSuccess, Realm.Transaction.OnError onError)

But via change listeners, you can't be certain what transaction's success you're listening to, just that you've modified the underlying table on some thread.

Of course, maybe writing to the Realm synchronously on the UI thread is strongly discouraged.

Realm version(s): 1.0.1

T-Enhancement

Most helpful comment

I can see the benefit of declaring these callback or code block. And it will make it easier to refactor between async. and sync. transaction.

All 5 comments

I am a bit torn about this.

1) Synchronous writes on the UI thread is highly discouraged.
2) You should register change listeners on the results not the transaction.

That said, I can see there is room for error here given how Realm works, but I am not sure this is the solutions. See e.g #2976

I'll have to think a little 馃槃

I do wonder if this can easily be resolved just by adding handler.post(new Runnable() {...}); by the user after the executeTransaction() call and this question is solved quite easily.

After thinking a little. The reason we wanted to make RealmResults "fixed" for the duration of a Looper event was to enable proper iteration behaviour. This however introduced the chance that if you did:

RealmResults results = getResults();
doTransaction();
// Here results still hold the result of the data before the transaction, meaning that new items are not present and deleted items are in the result set.

We knew of this trade-off when doing that, but it happening is not visible in the API, you need to read the website docs and Javadoc to realise that.

For that reason I can see benefits of having onSuccess/onFailure callbacks even on the sync transaction, it makes that behaviour more visible in the API itself. So guess I'm starting to be in favour of this. We can also add this without breaking the existing API.

Thoughts @realm/java?

I can see the benefit of declaring these callback or code block. And it will make it easier to refactor between async. and sync. transaction.

With Realm 3.0 and results integration that commitTransaction() calls change listeners, the proposal can change a bit:

The success callback could be called after all other threads have been updated successfully. This would be a blocking operation.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bryanspano picture bryanspano  路  3Comments

jjorian picture jjorian  路  3Comments

wezley98 picture wezley98  路  3Comments

mithrann picture mithrann  路  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  路  3Comments