Realm Swift iOS using shouldIncludeInDefaultSchema in Object class crashes app

I write and maintain an xcframework that uses Realm. As well, apps that use our framework would like to use Realm. Therefore, I want to exclude our Object subclasses from the default.realm schema. I have read postings that tout the use of: public override class func shouldIncludeInDefaultSchema() → Bool { return false} in an Object subclass. When I do this I consistently get a crash like this:

2023-01-10 12:18:28.008097-0800 AppIngo *** Terminating app due to uncaught exception ‘RLMException’, reason: ‘Object type ‘SomeEntity’ not managed by the Realm’

So my questions are:

  1. Is this a supported mechanism for keeping Object subclasses out of the default.realm? If so, is there anything thing we need to do make this work and not crash?
  2. Is there any other strategy/mechanism to achieve what we are looking for.

Hey @Tom_McHale - I’m one of the folks who write documentation for the Realm Swift SDK. I do use Realm myself in a couple of personal projects, but I haven’t tried using Realm in an xcframework, so I can’t answer your question directly. But I can offer you some information.

You may already know some of this based on your comment, but I just want to level set some baseline info.

The default behavior of the Realm Swift SDK is to add any Object, EmbeddedObject or AsymmetricObject-derived objects in a project to the realm file schema. Realm scans for these object types at runtime and adds them to the realm file schema.

If you want to open a realm that only uses a subset of these object types, you can explicitly pass objects using the Realm.configuration.objectTypes property. You can see a code example demonstrating this in our docs at: Provide a Subset of Classes to a Realm. When you pass one or more objects explicitly, this is mutually exclusive to the default behavior of adding all object types to the schema. So the net effect is to exclude all object types that you do not explicitly pass in the realm configuration.

You can explicitly set object types for any realm, including the default realm. So the easiest solution would probably be to have apps that use your framework explicitly manage the object types they use when opening a realm file. I would expect this to keep your framework objects out of the App realms.

It’s my understanding that the method you’re trying to use:

override class func shouldIncludeInDefaultSchema() -> Bool {
    return false

Is undocumented intentionally. I only learned that it existed a few months ago. I do not recall why it is undocumented intentionally - whether it’s because using it can lead to complications/crashes, whether it’s not fully supported or guaranteed to be available in future versions, or whether there are implications of using it that mean we don’t recommend using it in a production app.

I do use it successfully in our documentation unit test project - you can see it in action in our docs GitHub repository: AsymmetricSync.swift. In that file, I must explicitly pass the object type that I have excluded from schema discovery to the realm where I want to write it. I don’t know in your instance, but it’s possible that the crash you’re experiencing is because you are not explicitly passing the SomeEntity object type to the realm where you are attempting to read or write it?

Sorry I can’t give you a direct answer to your questions, but I hope this info helps!

Thank you for the answer. I will check out the AsymmetricSync example. I do think we are using Realm correctly. I should have added that we consume Realm via Carthage in an xcframework. Thx again.