Anonymous user in Realm Mobile Platform

拟墨画扇 提交于 2019-12-06 06:13:17

问题


Can I connect to a remote Realm without having to login?

In Swift, the only way to create a synchronizable Realm is through the syncConfiguration property of a Realm.Configuration. Is there a method for getting an anonymous User so that anyone can connect to the remote Realm?


回答1:


Can I connect to a remote Realm without having to login?

No, you always need to be authenticated.

Is there a method for getting an anonymous User so that anyone can connect to the remote Realm?

Yes, via SyncCredentials.anonymous().




回答2:


This is now possible in Realm Cloud. Here's how I am doing it in 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...
        }
      }
    }
  }
}

Good luck!



来源:https://stackoverflow.com/questions/39736929/anonymous-user-in-realm-mobile-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!