Firebase Remote Config results on initial request

余生颓废 提交于 2019-12-06 07:10:55

You need to call activateFetched() after the fetch completes. Right now, you're calling it before the fetch even starts. Fetched config parameters will not be available to your app until you call activateFetched().

With @doug-stevenson's answer, this is the code to fetch config parameters, and use them immediately:

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()
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!