简体   繁体   English

Swift App中出现了不同的领域配置

[英]Different Realm Configurations Appearing in Swift App

Swift 3, Xcode 8, RealmSwift 2.0.2, Realm Object Server 1.0 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. 在我的应用程序委托中,我有一个函数可以设置我的Realm配置以连接到已设置的远程同步服务器。 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. 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)!)
    )
 })

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. 我的假设是,在此处设置Realm配置意味着Realm()所有实例都将使用此配置。

I then set a realm object as a class property in two separate view controllers: 然后,我在两个单独的视图控制器中将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)
  }
}

...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. 如上面的代码注释所述, realm的两个实例是不同的。 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. 在其他视图控制器上,我看不到任何数据,因为它使用了错误的Realm数据库。

Can I not reliably expect a Realm configuration to persist throughout my app? 我不能可靠地期望Realm配置在我的应用程序中持久存在吗? 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. 如果在运行回调之前恰好创建了一个视图控制器子类的实例,则在您对身份验证完成处理程序进行的任何更改之前,打开的领域将使用默认的默认配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM