Realm-java: Switch from a Synchronous Transaction to an Asynchronous Transaction

Created on 16 Jan 2016  路  7Comments  路  Source: realm/realm-java

According to this realm doc.section, realm are advise to perform all "Realm write" operations on a background thread (not Android鈥檚 main thread), is that safe if i change directly to Asynchronous in entirely transaction ? which mean i will never use the synchronous transaction during the development.

T-Help

Most helpful comment

Generally I make a "realm repository" which does not manage transactions at all, just the writes. The writes are managed by whatever thread is trying to write that way, although I prefer to do these things with synchronous transaction on background thread.

     try(Realm r = Realm.getDefaultInstance()) {
         r.executeTransaction(realm -> {
             // do many writes
             repository.insertOrUpdate(realm, blah);
             otherRepository.insertOrUpdate(realm, bleh);
         });
     }

All 7 comments

I'm not sure what you mean by safe.

A write transaction is a blocking operations i.e., only one write operation can execute at a time and others will wait. Moreover, write transactions involve I/O activity, so we recommend that you perform most transactions not on the main thread.

Of course, async transactions might be a bit more complex to use, but to simplify your code, you can use executeTransaction().

Hello Realm Team,

I have a similar question:

Suppose I declare a method containing some database operation.
What's the better approach: declaring an asynchronous transaction in the method or declaring a synchronous transaction.
I personally feel that the latter provides more flexibility, since now I have the option to run it in the same thread as well as another thread!
what do you guys suggest?
Thanks!

Generally I make a "realm repository" which does not manage transactions at all, just the writes. The writes are managed by whatever thread is trying to write that way, although I prefer to do these things with synchronous transaction on background thread.

     try(Realm r = Realm.getDefaultInstance()) {
         r.executeTransaction(realm -> {
             // do many writes
             repository.insertOrUpdate(realm, blah);
             otherRepository.insertOrUpdate(realm, bleh);
         });
     }

@Zhuinden thanks for your time! :)
I also follow a similar pattern and strategy. :)

Just as reminder Closing Realm Instance @SarthakM9

repository

Generally I make a "realm repository" which does not manage transactions at all, just the writes. The writes are managed by whatever thread is trying to write that way, although I prefer to do these things with synchronous transaction on background thread.

     try(Realm r = Realm.getDefaultInstance()) {
         r.executeTransaction(realm -> {
             // do many writes
             repository.insertOrUpdate(realm, blah);
             otherRepository.insertOrUpdate(realm, bleh);
         });
     }

Yeah a good one. Can you post some more info on having a separate repository and running this on background thread please?

Was this page helpful?
0 / 5 - 0 ratings