初始请求的 Firebase 远程配置结果

Firebase Remote Config results on initial request

我正在使用单例从 Firebase 远程配置文件中获取参数。第一次应用是运行,我只能从单例访问默认值;随后 运行s 正确 return 配置的值。执行此操作的更好方法是什么,以便我可以从新开始访问这些值?

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命令returns(大概带有参数值)时,我仍然无法访问来自配置文件的这些值。只有在应用程序的后续 运行 之后,它们才会出现。这是为什么?我的代码中是否遗漏了什么?

您需要在 提取完成后调用 activateFetched() 。现在,您称它为 before 获取甚至开始。在您调用 activateFetched() 之前,您的应用程序无法使用获取的配置参数。

根据@doug-stevenson 的回答,这是获取配置参数并立即使用它们的代码:

protocol RemoteConfigProtocol {
    func remoteConfigReceived()
}

class RemoteConfigManager {

    static let sharedInstance = RemoteConfigManager()
    var delegate: RemoteConfigProtocol?

    var 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)

        // set the values from the defaults
        self.demoTitle = remoteConfig["demo_title"].stringValue!

        // fetch the new values
        remoteConfig.fetch(withExpirationDuration: expirationDuration) { status, _ in
            print("Fetch completed with status:", status, "(\(status.rawValue))")

            // activate the newly fetched values
            remoteConfig. activateFetched()

            // reset my variables
            self.demoTitle = remoteConfig["demo_title"].stringValue!

            // tell my view controller to update the UI
            self.delegate?.remoteConfigReceived()
        }
    }
}