noamtm
noamtm

Reputation: 12953

Realm: Multiple instances, each with own config

My application uses two separate Realm instances (the second comes from a library).

The application itself uses Realm.Configuration.defaultConfuguration and the library creates its own configuration (Realm.Configuration(...)).

On runtime (after inspecting with Realm Browser), we see that both instances (each live in its own file) contain the models from both Realms. This, of course, has implications on migrations.

I know that when both use the same configuration we should set configuration.objectTypes, but I didn't expect it to matter when each instance has its own configuration.

How can two distinct configs share any data between them? It seems like a bug in Realm - or maybe I'm missing something.

Upvotes: 3

Views: 752

Answers (1)

noamtm
noamtm

Reputation: 12953

An explanation was posted in Realm's issues on GitHub. I'm copying the response here for future searches:

By default objectTypes will include all RealmSwift.Object subclasses, regardless of where they are defined.

A library which uses Realm should override shouldIncludeInDefaultSchema() to exclude its types from the default objectTypes (i.e. add public override class func shouldIncludeInDefaultSchema() -> Bool { return false } to the class definitions) and then explicitly list the types it uses. This lets any applications using the library continue to simply use the automatic class discovery.

The credit goes to Thomas Goyne (Realm developer).

Upvotes: 2

Related Questions