Swift App中出现了不同的领域配置 [英] Different Realm Configurations Appearing in Swift App

查看:113
本文介绍了Swift App中出现了不同的领域配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift 3,Xcode 8,RealmSwift 2.0.2,Realm Object Server 1.0

在我的应用程序委托中,我有一个函数可以设置我的Realm配置以连接到已设置的远程同步服务器.我只是使用一个测试帐户进行身份验证,直到我了解同步的基础知识为止. 1.1.1.1不是我的真实IP地址. ;)

let username = "test"
let password = "test"

let address = "http://1.1.1.1:9080"
let syncAddress = "realm://1.1.1.1:9080/~/myapp"

SyncUser.authenticate(with: Credential.usernamePassword(username: username, password: password, actions: []), server: URL(string: address)!, onCompletion: { user, error in
    guard let user = user else {
      fatalError(String(describing: error))
    }

    // Open Realm
    Realm.Configuration.defaultConfiguration = Realm.Configuration(
      syncConfiguration: (user, URL(string: syncAddress)!)
    )
 })

这似乎工作正常.我看到数据出现在服务器上,并且没有错误. 我的假设是,在此处设置Realm配置意味着Realm()的所有实例都将使用此配置.

然后在两个单独的视图控制器中将realm对象设置为类属性:

class TableViewControllerA: UITableViewController{
  let realm = try! Realm()
  override func viewDidLoad() {
    // CORRECT: Prints "nil" as it should for a remotely synced Realm instance
    print(realm.configuration.fileURL)
  }
}

...以及另一个文件中的另一个

:

class ViewControllerB: UIViewController{
  let realm = try! Realm()
  override func viewDidLoad() {
    // WRONG: Prints the path to the local realm file in the Simulator
    print(realm.configuration.fileURL) 
  }
}

如上面的代码注释中所述,realm的两个实例是不同的.在某些视图控制器上,我可以将对象保存到服务器,然后将其显示在设备上.在其他视图控制器上,我看不到任何数据,因为它使用了错误的Realm数据库.

我不能可靠地期望Realm配置在我的应用程序中持久存在吗?我需要做其他事情才能使用相同的配置吗?

解决方案

您正在身份验证完成处理程序中设置默认配置.用户通过身份验证后,将异步调用此回调.如果在运行回调之前恰好创建了一个视图控制器子类的实例,则在您对身份验证完成处理程序进行的任何更改之前,打开的领域将使用 default 默认配置./p>

Swift 3, Xcode 8, RealmSwift 2.0.2, Realm Object Server 1.0

In my app delegate, I have a function that sets my Realm configuration to connect to a remote sync server I have set up. I'm just using a test account to authenticate until I can get the basics of sync working. 1.1.1.1 isn't my real IP address. ;)

let username = "test"
let password = "test"

let address = "http://1.1.1.1:9080"
let syncAddress = "realm://1.1.1.1:9080/~/myapp"

SyncUser.authenticate(with: Credential.usernamePassword(username: username, password: password, actions: []), server: URL(string: address)!, onCompletion: { user, error in
    guard let user = user else {
      fatalError(String(describing: error))
    }

    // Open Realm
    Realm.Configuration.defaultConfiguration = Realm.Configuration(
      syncConfiguration: (user, URL(string: syncAddress)!)
    )
 })

This seems to work fine. I see data appear on my server, and I get no errors. My assumption is that setting the Realm configuration here means that all instances of Realm() will use this configuration.

I then set a realm object as a class property in two separate view controllers:

class TableViewControllerA: UITableViewController{
  let realm = try! Realm()
  override func viewDidLoad() {
    // CORRECT: Prints "nil" as it should for a remotely synced Realm instance
    print(realm.configuration.fileURL)
  }
}

...and another in another file:

class ViewControllerB: UIViewController{
  let realm = try! Realm()
  override func viewDidLoad() {
    // WRONG: Prints the path to the local realm file in the Simulator
    print(realm.configuration.fileURL) 
  }
}

As noted in the code comments above, the two instances of realm are different. On some of my view controllers, I can save objects to the server and see them appear on my device. On other view controllers, I don't see any data because it's using the wrong Realm database.

Can I not reliably expect a Realm configuration to persist throughout my app? Do I need to do something else to use the same configuration?

解决方案

You're setting the default configuration within the authentication completion handler. This callback is invoked asynchronously after the user has been authenticated. If an instance of one of your view controller subclasses happens to be created before the callback runs, the Realm it opens will use the default default configuration, prior to any changes you make in your authentication completion handler.

这篇关于Swift App中出现了不同的领域配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆