【发布时间】:2016-12-14 08:55:51
【问题描述】:
在我的应用程序中,我正在管理多个领域数据库文件,就是这样,对于每个登录的用户,都存在一个领域文件 (.realm)。
因此,当用户登录应用程序时,我正在执行以下操作:
class func setDefaultsForLocationId(_ userId: String) {
var config = RealmManager.realmConfiguration()
// Use the default directory, but replace the filename with the business id
config.fileURL = config.fileURL!.deletingLastPathComponent()
.appendingPathComponent("\(userId).realm")
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
}
完成后,我开始使用以下方法添加到领域:
fileprivate func storeTransaction(_ student: Student) -> Bool {
let realm = try! Realm()
var retVal = true
do {
try realm.write {
realm.add(student, update: true)
}
} catch {
retVal = false
}
return retVal
}
在当前用户注销并且新用户登录之前,这将正常工作,将引发异常: 无法添加来自不同领域的对象。
注意 1:我一次只使用一个领域实例,而不是同时使用来自不同 .realm 文件的多个实例。
注意2:我发现如果我使用同一个帐户注销和登录;只有当我使用不同的帐户时才会出现异常!
【问题讨论】: