【问题标题】:Firebase Remote Config results on initial requestFirebase 远程配置在初始请求时产生结果
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 是的!此外,我的变量需要在此之后重置。谢谢!
    【解决方案2】:

    根据@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()
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-21
      • 2016-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多