I have two Realm files configured in my app. I want to store my Log model to a separate file from the rest of my models. My problem is that I also see my Log model class in my default Realm file, which I don't want. How can I exclude a particular model class from a given Realm file?
I use the default configuration for my main Realm file, and I want to store the Log model only in another database file, but when I default.realm in the Realm Browser it also shows the Log model.
You can explicitly list the classes a given Realm can store via the objectTypes property on Realm.Configuration:
let configA = Realm.Configuration(fileURL: realmFileURL,
objectTypes: [Dog.self, Owner.self])
let realmA = Realm(configuration: configA)
let configB = Realm.Configuration(fileURL: otherRealmFileURL,
objectTypes: [Log.self])
let realmB = Realm(configuration: configB)
realmA can only store instances of Dog and Owner, while realmB can only store instance of Log.
You can override this method in unmanaged classes
public class Log: Real.Object .... {
...
...
public override static func shouldIncludeInDefaultSchema() -> Bool {
return false
}
}
you can now create your realm with the default settings
let realm = Realm()
来源:https://stackoverflow.com/questions/43604694/exclude-a-realm-model-class