1
在我的应用程序中,我正在管理多个领域数据库文件,就是这样,每个登录用户都有一个领域文件(.realm)存在。无法添加来自其他领域的对象iOS和Swift
因此,当用户登录到应用程序我做了以下内容:
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:我发现如果我使用同一个帐户注销并登录,只有当我使用不同的帐户时才会出现异常!
一般来说,我一次只使用一个领域,我不能同时处理来自不同.realm文件的多个领域实例。 我想知道是否有办法在内存刷新领域! –
独立的想法不起作用!它产生同样的例外 –
也许'学生'有其他对象作为关系吗?如果是这样,你应该递归地分离关系对象。无论如何,既然你不能同时处理多个领域,你应该在切换领域时清除旧的领域物品。所以你应该在切换域后重新创建领域并重新获取领域对象。你能说明如何调用'storeTransaction()'方法吗? –