我正在尝试为登录用户设置领域。我在领域文档中发现可以通过以下方式做到这一点:
func setDefaultRealmForUser(username: String) {
var config = Realm.Configuration()
// Use the default directory, but replace the filename with the username
config.fileURL = config.fileURL!.deletingLastPathComponent().appendingPathComponent("\(username).realm")
// Set this as the configuration used for the default Realm
Realm.Configuration.defaultConfiguration = config
}
我想要做的是添加安全应用程序组标识符。总而言之,一切看起来像这样:
func setDefaultConfigForUser(username: String) -> Realm.Configuration {
var config = Realm.Configuration()
guard let directory: URL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "someIdentifier")?.appendingPathComponent("xyz.realm") else {
fatalError("ERROR!")
}
config.fileURL = directory.deletingLastPathComponent().appendingPathComponent("\(username).realm")
Realm.Configuration.defaultConfiguration = config
return config
}
然后我只声明配置,实例化领域并尝试打开它,但是当我进入应用程序的领域文件时,该用户没有领域文件,而只有一个默认文件(default.realm )
let config = setDefaultConfigForUser(username: user.login!)
_ = try! Realm(configuration: config)