Realm with many observe & write transactions on app launch. Performance problems


Our app is doing many write transactions on app launch, but all of them are done in separated background threads, and at that time our view models observing necessary results or objects from main thread. These observations are blocking main thread. Just need clarification(s) that, how to manage observe operations while realm is in write transaction. I have few ideas about this situation;

  • Observe from background. Different run loop \ thread by using ThreadSafeReference

  • Giving high priority to observe operations, make sure write transactions will start after all observations done

  • Parallel observe or write in app general

Just I need to be clear how to manage this kind of situation.

Thank you (:


I dont really know the 100% answer, but lets think together:

Some facts:

  • notifications will be sent to the same thread
  • with threadSafeReference, you can pass objects from one thread to an another

In the documentation it says:

Notifications are always delivered on the thread that they were originally registered on. That thread must have a running Looper. If the relevant write transaction happened on a different thread, the listener will be called asynchronously after the transaction is committed.

Which means (i think) that you will still get a notification of a write transaction in an another thread, just a bit later. I think you can force it with a real.refresh() call after the write transaction.

So, basically you will need:

  • Thread1: with write transactions
  • Thread2: get the result from the realm. The object change notification will still be arrived here

You wont need threadSafeReference, as the models are not changing threads.