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()'方法嗎? –