gpt4 book ai didi

ios - 如何为 Realm 设置默认的 SyncConfiguration,以便我可以在多个 ViewController 中获取它而无需冗余代码?

转载 作者:行者123 更新时间:2023-11-30 12:21:34 25 4
gpt4 key购买 nike

根据:

我的方法是这样的:

AppDelegate.swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
DispatchQueue.main.async {
let username = "test@test.com"
let password = "Test123"
let serverUrl = URL(string: "http://test.com:9080")
let realmUrl = URL(string: "realm://test.com:9080/~/realmtest")

if let user = SyncUser.current {
Realm.Configuration.defaultConfiguration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmUrl!)
} else {
SyncUser.logIn(with: .usernamePassword(username: username, password: password, register: false), server: serverUrl!, onCompletion: { (user, error) in
guard let user = user else {
print("Error: \(String(describing: error?.localizedDescription))")
return
}
Realm.Configuration.defaultConfiguration.syncConfiguration = SyncConfiguration(user: user, realmURL: realmUrl!)
})
}
}

return true
}

ViewController.swift

override func viewDidLoad() {
super.viewDidLoad()
print("SyncConfiguration: \(String(describing: Realm.Configuration.defaultConfiguration.syncConfiguration))")
self.realm = try! Realm()
}

当我第一次打开应用程序时,没有任何反应,但当我第二次打开应用程序时,Realm 工作正常。

每当我打开应用程序时,打印的 SyncConfigurationnil。没有错误!

到处搜索但找不到答案...

最佳答案

问题是您正在使用异步方法来配置您的 Realm ,但您没有在方法的完成处理程序中调用打印。您应该只在异步调用完成后才显示 View Controller 。

关于ios - 如何为 Realm 设置默认的 SyncConfiguration,以便我可以在多个 ViewController 中获取它而无需冗余代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44730334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com