realm-helpers
realm-helpers copied to clipboard
Request for Feature: Add Log for opened Realm in the RealmManager
I was thinking about a feature which makes it possible for the developer to see some sort of information in the logcat. For example such this information:
- The number of open Realms (Usage of
openLocalInstance) - The name/id of the thread and type of the thread which the realm opened inside when calling
getLocalInstance - The name/id of the thread when calling
closeLocalInstanceand the number of remains opened Realms. - Name of the Class/Activity/... which opened the Realm for its usage.
- Or such this useful information.
Not a bad idea, but an even better idea is to just make the Realm lifecycle management less convoluted. You have to remember open, close and get and where you can and cannot call these methods - sounds tricky no?
If you're feeling adventurous, you could check out this thing I wrote in the past 3 hours which I've dubbed Monarchy, totally incomplete and not yet exposed as a library either, but it seems to be working.
It listens for Realm on a background looper thread, and copies the query results and sends them to UI thread through LiveData.
Realm lifecycle management is handled entirely by subscription. Or at least that's the theory. If you need a Realm as is, you can use doWithRealm() which gives you a Realm for that block, but not outside of it.
It's really great. But
- With the Monarchy, the developer's work becomes easier and make less boilerplate code and minimize the exceptions.
- With the requested feature, the developer will know if he does it right at all?! (Specifically in some usages like MVP architecture and DI). Honestly, The usefulness of this requested feature is most often the first times we want to use the Realm in our project architecture.
Let's close this issue and maybe someday, I'll develop it by myself and send a pull request. :construction_worker_man:
Nah, I should maintain this one for now too, but that's how I truly envision a global singleton Realm manager :smile: the other one doesn't support managed RealmResults on the UI thread by design, and that might not be what you need
But if you add a PR I'll definitely look into it!
I run my project under debugging mode and I found this:

Does the difference between localRealms thread reference and realm thread reference mean I did use RealmManager in a wrong way? Or the only important thing here is threadId?
No this looks fine to me
BTW check out https://medium.com/@Zhuinden/introducing-monarchy-the-rulership-over-the-realm-api-with-the-help-of-livedata-b394022bd916 for an alternative approach for lifecycle things
Haha :smile: , Another good tutorial on this subject, finally. :muscle: