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

Ios 初始请求时的Firebase远程配置结果,ios,firebase,firebase-remote-config,Ios,Firebase,Firebase Remote Config,我使用单例从Firebase远程配置文件获取参数。第一次运行应用程序时,我只能从singleton访问默认值;后续运行将正确返回配置的值。有什么更好的方法可以做到这一点,这样我就可以从一个新的开始访问这些值 protocol RemoteConfigProtocol { func remoteConfigReceived() } class RemoteConfigManager { static let sharedInstance = RemoteConfigManage

我使用单例从Firebase远程配置文件获取参数。第一次运行应用程序时,我只能从singleton访问默认值;后续运行将正确返回配置的值。有什么更好的方法可以做到这一点,这样我就可以从一个新的开始访问这些值

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

当asynchronous
fetch
命令在上面的代码中返回时(可能带有参数值),我仍然无法访问来自配置文件的这些值。只有在应用程序的后续运行中,它们才会出现。为什么呢?我的代码中缺少什么吗?

在提取完成后,需要调用activateFetched()。现在,你要在抓取开始之前打电话。在您调用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()
        }
    }
}

对加上我的变量需要在这一点之后重置。非常感谢。