Realm 移动平台中的匿名用户
Anonymous user in Realm Mobile Platform
我可以在不登录的情况下连接到远程领域吗?
在 Swift 中,创建可同步领域的唯一方法是通过 Realm.Configuration
的 syncConfiguration
属性。是否有获取匿名用户的方法,以便任何人都可以连接到远程领域?
Can I connect to a remote Realm without having to login?
不,您始终需要进行身份验证。
Is there a method for getting an anonymous User so that anyone can connect to the remote Realm?
是的,通过 SyncCredentials.anonymous()
。
现在可以在 Realm Cloud 中实现。这是我在 Swift:
中的做法
if let user = SyncUser.current {
//--== User available ==--
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: "..."))
Realm.Configuration.defaultConfiguration = config
let _ = try! Realm()
}else{
//--== No User; Connect Anonymously ==--
let credentials = SyncCredentials.anonymous()
SyncUser.logIn(with: credentials, server: "...") { user, error in
DispatchQueue.main.async {
if let user = user {
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: "..."))
Realm.Configuration.defaultConfiguration = config
let _ = try! Realm()
}else{
//Error...
}
}
}
}
}
祝你好运!
我可以在不登录的情况下连接到远程领域吗?
在 Swift 中,创建可同步领域的唯一方法是通过 Realm.Configuration
的 syncConfiguration
属性。是否有获取匿名用户的方法,以便任何人都可以连接到远程领域?
Can I connect to a remote Realm without having to login?
不,您始终需要进行身份验证。
Is there a method for getting an anonymous User so that anyone can connect to the remote Realm?
是的,通过 SyncCredentials.anonymous()
。
现在可以在 Realm Cloud 中实现。这是我在 Swift:
中的做法if let user = SyncUser.current {
//--== User available ==--
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: "..."))
Realm.Configuration.defaultConfiguration = config
let _ = try! Realm()
}else{
//--== No User; Connect Anonymously ==--
let credentials = SyncCredentials.anonymous()
SyncUser.logIn(with: credentials, server: "...") { user, error in
DispatchQueue.main.async {
if let user = user {
let config = Realm.Configuration(syncConfiguration: SyncConfiguration(user: user, realmURL: "..."))
Realm.Configuration.defaultConfiguration = config
let _ = try! Realm()
}else{
//Error...
}
}
}
}
}
祝你好运!