【发布时间】:2018-02-05 18:39:33
【问题描述】:
我正在使用单例从 Firebase 远程配置文件中获取参数。第一次运行应用程序时,我只能从单例中访问默认值;后续运行正确返回配置的值。有什么更好的方法可以做到这一点,以便我可以从头开始访问这些值?
protocol RemoteConfigProtocol {
func remoteConfigReceived()
}
class RemoteConfigManager {
static let sharedInstance = RemoteConfigManager()
var delegate: RemoteConfigProtocol?
let demoTitle: String
// private initialiser for the singleton
private init() {
// Configure for dev mode, if needed
let remoteConfig = RemoteConfig.remoteConfig()
#if DEBUG
let expirationDuration: TimeInterval = 0
remoteConfig.configSettings = RemoteConfigSettings(developerModeEnabled: true)!
#else
let expirationDuration: TimeInterval = 3600
#endif
// default values
let appDefaults: [String: NSObject] = [
"demo_title": "Default Title" as NSObject
]
remoteConfig.setDefaults(appDefaults)
// what exactly does "activeFetched" do, then?
remoteConfig.activateFetched()
// set the values
self.demoTitle = remoteConfig["demo_title"].stringValue!
// this seems to prep app for subsequent launches
remoteConfig.fetch(withExpirationDuration: expirationDuration) { status, _ in
print("Fetch completed with status:", status, "(\(status.rawValue))")
self.delegate?.remoteConfigReceived()
}
}
}
当异步fetch 命令在上面的代码中返回时(可能带有参数值),我仍然无法访问来自配置文件的这些值。只有在应用程序的后续运行时,它们才会出现。这是为什么?我的代码中是否缺少某些内容?
【问题讨论】:
标签: ios firebase firebase-remote-config