The kingdom no longer has this problem.
For reference, my solution at that time was:
Thanks to beeender for pointing me in the right direction and linking this PR https://github.com/realm/realm-java/pull/1297
Question
When there is a transaction with a pending Realm transaction, any call to Realm.getInstance in another thread is blocked until the pending transaction is completed or canceled.
In my case, I have an IntentService that populates my Realm with existing user data, while I try to display any current data by querying Realm in the user interface thread. Although the requests are simple and do not cause any problems normally, if there is a pending transaction in the IntentService, the call to Realm.getInstance will be blocked, blocking the UI thread that potentially calls ANR.
My first attempt at a solution was to pull out a branch of the beeender PR team and create a jar. I believe this fix helped me take another step, allowing me to create an instance of Realm without blocking, but the UI thread was still blocked by small transactions that I tried to execute in the UI thread.
Decision
The implemented solution includes several stages:
- Create repeating objects for all my models. Duplicates do not distribute RealmObject (since RealmObjects cannot be used on streams.)
- Move all calls in Realm to background threads. I basically wrapped my queries in AsyncTasks and added listeners that return a version of the model other than RealmObject.
- Make smaller transactions, not less large transactions. Where previously I started and made transactions on both sides of the cycle that created many new RealmObjects, now I start and commit the transaction for each object. The goal of this is to reduce the total continuous time during which Realm can be in an open transaction state, so my requests providing data for the user interface can complete without having to wait.
Conclusion
I initially did not dare to use Realm due to the fact that it is still in beta, as well as a warning that RealmObjects cannot be used in streams. After some tests, I was sure that I could perform simple queries on the user interface thread without problems (still with guilt in my gut).
A common kingdom is a great project to follow, but I feel that it is not ready for large-scale commercial projects. Using Realm in this project may have saved time, but it cost many dissatisfied customers and it is difficult to diagnose the problem.
* Edit: Clarified question.
source share