[英]Synced Realm not being loaded on launch
I'm attempting to use Realm Object Server in my ios app. 我正在尝试在ios应用程序中使用Realm Object Server。 I am hosting it on AWS, and I can successfully read and write to it. 我将其托管在AWS上,并且可以成功对其进行读写。 However, I'm having a problem accessing the synced realm after launching my app. 但是,启动我的应用程序后,访问同步领域时遇到问题。 On launch, I have a login view controller (very similar to the one provided in the example app) 在启动时,我有一个登录视图控制器(非常类似于示例应用程序中提供的那个)
After authenticating successfully, I want to access the synced Realm to decide which view controller to present. 成功进行身份验证后,我想访问同步的Realm来确定要显示的视图控制器。
@IBAction func logIn(sender: AnyObject?) {
guard userInputValid() else {
return
}
self.activityIndicator.startAnimating()
self.logInButton.isHidden = true
SyncUser.authenticate(with: Credential.usernamePassword(username: userNameTextField.text!, password: passwordTextField.text!, actions: []), server: syncAuthURL!, onCompletion: { user, error in
guard let user = user else {
self.presentError(error: error as! NSError)
self.activityIndicator.stopAnimating()
self.logInButton.isHidden = false
return
}
Realm.Configuration.defaultConfiguration = Realm.Configuration(
syncConfiguration: (user, self.syncServerURL!),
objectTypes: [Dot.self, Mod.self]
)
let realm = try! Realm()
// realm.objects(Dot.self).count == 0 here even though there is a Dot object persisted in the server
// When I access the realm later in the app (e.g in a viewDidAppear of another controller) the object is there
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let vc = DotCollectionViewController(nibName: "DotCollectionViewController", bundle: nil)
let navVC = UINavigationController(rootViewController: vc)
appDelegate.window?.rootViewController = navVC
})
}
I have validated reads and writes to the synced Realm and found something peculiar: the realm seems to have a fileURL of null even when I'm able to write data successfully. 我已经验证了对同步的Realm的读写,并发现了一些奇怪的东西:即使我能够成功写入数据,该领域似乎也具有null的fileURL。 I am accessing the realm in other parts of my app using let realm = try! Realm()
我正在使用let realm = try! Realm()
来访问应用程序其他部分的let realm = try! Realm()
let realm = try! Realm()
and when I do print(realm.configuration.fileURL)
I always get nil, even for valid synced Realms. let realm = try! Realm()
,当我执行print(realm.configuration.fileURL)
,即使对于有效的同步Realms,我也总是nil。
So my question is, do I have to wait some period of time for the synced Realm to be accessible? 所以我的问题是,是否需要等待一段时间才能访问同步的Realm? If so, how would I go about doing this? 如果是这样,我将如何去做? Any help on this issue would be much appreciated. 在这个问题上的任何帮助将不胜感激。
It's expected that the fileURL
of a synced Realm's configuration be nil
, since the file location is managed my Realm and inaccessible to the user (you). 预计同步Realm配置的fileURL
为nil
,因为文件位置是由Realm管理的,并且用户(您)无法访问。
When opening a synced Realm for the first time on a device, the Realm will be accessible as soon as a valid SyncUser
exists, provided by the SyncUser.authenticate(...)
callback. 首次在设备上打开同步的Realm时,只要SyncUser.authenticate(...)
回调提供了有效的SyncUser
,就可以访问该Realm。 However, its contents will be populated asynchronously, which is why realm.objects(Dot.self)
is empty when immediately opening the Realm, but later gets the objects from the server. 但是,其内容将异步填充,这就是为什么当立即打开Realm时realm.objects(Dot.self)
为空,但后来又从服务器获取对象的原因。
If it's important to wait for that data to be available before proceeding with a part of your app, there are two ways Realm is working on to do that in the near future: 如果在继续处理应用程序的一部分之前等待数据可用很重要,那么Realm有两种方法可以在不久的将来做到这一点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.