When would I use multiple realms in a single application?

I am currently looking to migrate my hobby flutter project from using sqlite to realm since I am very interested in the strong offline first support that realm offers with the ease of switching to sync at a later time.

The app consists of several modular trackers for things like recipes, shopping lists, budgets etc. Each module is independent of one another so data will not be related, at least not at the persistence layer.

Given that, would it make sense to use a realm per module? A followup question is if adding dozens of schemas to a single realm will impact performance significantly?

Hi Scott

There is no performance advantage of opening a separate realm per module.

There are many downsides, fx. transactions cannot span different realms.

Hence I would suggest opening just a single realm and pass it to each module.

If there are conceptual links between the various model classes, I would suggest modelling these explicitly, but you don’t have to.

On a more general note…

When starting a realm project, especially migrating from something like sqlite, I often see people hide the database layer, treating the objects returned as plain old dart objects (PODOs), calling toList on RealmResults, copying data from RealmObjects into non-realm objects that are then passed up the stack, and vice versa. That kind of architectural abstraction has a lot preconceptions about how a database work.

It will work with Realm as well, but it is not the best way to use it, as you loose the lazy loaded nature, and the ability to listen for changes. So give that some thought.

1 Like

Hi Kasper,

Thank you for your response. That makes sense and helps with how I will structure the app.

When starting a realm project, especially migrating from something like sqlite, I often see people hide the database layer, treating the objects returned as plain old dart objects (PODOs), calling toList on RealmResults , copying data from RealmObject s into non-realm objects that are then passed up the stack, and vice versa. That kind of architectural abstraction has a lot preconceptions about how a database work.

Sure enough, that was the approach I was taking at first. Your demo over at the Flutter Observable show (really helpful by the way) actually made me aware of the lazy-loaded aspect of Realm objects and wasn’t sure how I would tackle that come time to implement. I think those properties of Realm actually reduce some of complexities in the app when it comes to layer abstractions.

Thanks for the help!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.