Firebase Remote Config results on initial request

穿精又带淫゛_ 提交于 2019-12-22 18:05:39

问题


I'm using a singleton to fetch the parameters from a Firebase remote config file. The first time the app is run, I can only access the default values from the singleton; subsequent runs correctly return the config's values. What's a better way to do this, so I can access the values from a fresh start?

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()
        }
    }
}

When the asynchronous fetch command returns in the code above (presumably with the parameter values), I am still unable to access these values coming from the config file. Only upon subsequent runs of the app are they there. Why is that? Am I missing something in my code?


回答1:


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().




回答2:


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()
        }
    }
}


来源:https://stackoverflow.com/questions/48629338/firebase-remote-config-results-on-initial-request

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