realm-helpers icon indicating copy to clipboard operation
realm-helpers copied to clipboard

Request for Feature: Add Log for opened Realm in the RealmManager

Open Drjacky opened this issue 7 years ago • 7 comments

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 closeLocalInstance and the number of remains opened Realms.
  • Name of the Class/Activity/... which opened the Realm for its usage.
  • Or such this useful information.

Drjacky avatar Dec 13 '17 06:12 Drjacky

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.

Zhuinden avatar Dec 17 '17 05:12 Zhuinden

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:

Drjacky avatar Dec 17 '17 06:12 Drjacky

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!

Zhuinden avatar Dec 17 '17 10:12 Zhuinden

I run my project under debugging mode and I found this: image

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?

Drjacky avatar Dec 23 '17 10:12 Drjacky

No this looks fine to me

Zhuinden avatar Dec 23 '17 11:12 Zhuinden

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

Zhuinden avatar Dec 23 '17 11:12 Zhuinden

Haha :smile: , Another good tutorial on this subject, finally. :muscle:

Drjacky avatar Dec 24 '17 07:12 Drjacky