Using Realm in a Swift framework creates problems for iOS host applications, also using Realm

I am working on a closed source Swift framework that uses Realm internally. In the process of testing this structure, I use the framework in the iOS application, which also uses Realm.

When the environment creates its own Realm, it uses the framework-specific Realm.Configuration. The host application creates Realm objects using the default Realm configuration. Areas are "separate."

There are several problems:

  • When the host application creates a Realm link, the base database ends with tables for all classes of the Realm Object class that are inside the frame. This provides some knowledge about the insides of our structure for everyone who uses our structure and creates their own Kingdom.
  • Since all tables are created in all Realms, a problem arises when migration is required. Inside our environment, we can provide port for launch, but when the host application creates it Realm, and the internal classes of the framework are updated, there is no migration to start the host application, and the application crashes with a migration error.

Can I use two completely separate Realms in one application?

I can specify a list of classes for the Realm framework when using the ObjectTypes RealmConfiguation option, but this will require that any user of our infrastructure do the same. This seems unreasonable and also still allows 1. above to be a problem.

+5
source share
1 answer

Yes, it is possible to have two separate libraries / frameworks / code bases in the application, using Realm independently, and completely save your model objects.

If you have a collection of subclasses of Realm Object that you would like to keep confidential, you can mark them so that they are not automatically added to the default application schema by adding the following static override method:

 public class MyModel: Object { public override class func shouldIncludeInDefaultSchema() -> Bool { return false } } 

This class will only be added to the Realm database if it is explicitly declared in the objectTypes property of this Realm Configuration object.

Thus, you can isolate all your own classes of the Realm model only from your own structure, and the parent application can use Realm in the original context without adding any objects to it.

+1
source

Source: https://habr.com/ru/post/1266744/


All Articles