Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Realm in a Swift framework creating challenges for host iOS apps also using Realm

Tags:

ios

swift

realm

I'm working on a closed source Swift framework that uses Realm internally. In the process of testing this framework, I'm using the framework within an iOS app that also uses Realm.

When the framework creates it's own Realm, it uses a Realm.Configuration specific to the framework. The host app creates Realm objects using the default Realm configuration. The Realms are 'separate'.

There are a couple problems:

  1. When the host app creates a Realm reference, the underlying database ends up with tables for all the private Realm Object classes that are inside the framework. This exposes some knowledge of the internals of our framework to anyone that uses our framework and creates their own Realm.
  2. Because all tables are getting created in all Realms, there is a problem when a migration is required. Inside our framework, we can provide a migration to run, but when the host app creates it's Realm, and the framework internal classes are updated, there is no migration for the host app to run, and the app crashes with a migration error.

Is there any way to use two completely separate Realms within one app?

I may be able to specify the list of classes for the framework Realm when using RealmConfiguation's objectTypes option, but that would require any user of our framework to do the same. That seems unreasonable, and also still allows 1. above to be a problem.

like image 223
asutula Avatar asked Apr 14 '17 20:04

asutula


People also ask

What is realm for Swift?

Realm Swift is an easy to use alternative to SQLite and Core Data that makes persisting, querying, and syncing data as simple as working directly with native Swift objects. Deploy a sample appView documentation.

What is realm database IOS?

Realm is a fast, scalable alternative to SQLite with mobile to cloud data sync that makes building real-time, reactive mobile apps easy. Deploy a sample appView documentation.

How do I close a realm in Swift?

There is no need to manually close a realm in Swift or Objective-C. When a realm goes out of scope and is removed from memory due to ARC , the realm is closed.


1 Answers

Yes, it's possible to have two separate libraries/frameworks/codebases in an app use Realm independently and keep their model objects completely separate.

If you have a collection of Realm Object subclasses that you would like to keep private, you can mark them so they won't automatically be added to the app's default schema by adding the following static method override:

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

This class will then only be added to a Realm database if it is explicitly declared in the objectTypes property of that Realm's Configuration object.

This way, you can isolate all of your own Realm model classes to just your own framework, and the parent app can use Realm in the original context without any objects getting mixed up in it.

like image 94
TiM Avatar answered Oct 01 '22 12:10

TiM