exclude a Realm model class

我怕爱的太早我们不能终老 提交于 2019-12-06 07:30:25

问题


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.


回答1:


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.




回答2:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!