0

我在初始 ViewController viewDidLoad 中加载了以下代码。它最初工作正常。但它不应该每 10 秒查找一次更改吗?

当我在 Firebase 中更新配置值并发布时,我没有在应用程序中看到这一点。我在调试模式下运行,所以节流不是问题。

如果我重新启动应用程序,我会看到新值。由于间隔设置为 10 秒,我不应该在应用程序运行时看到更新吗?

let rc = FIRRemoteConfig.remoteConfig()

let interval: TimeInterval = 10
    FIRRemoteConfig.remoteConfig().fetch(withExpirationDuration: interval) {
        (status, error) in

        guard error == nil else {
            //handle error here
            return
        }

        FIRRemoteConfig.remoteConfig().activateFetched()
        let test = rc["key1"].stringValue //this runs only once
    }

任何想法为什么这不是更新?

4

1 回答 1

1

你应该scheduledTimer改用。

/// Fetches Remote Config data and sets a duration that specifies how long config data lasts.
    /// Call activateFetched to make fetched data available to your app.
    /// @param expirationDuration  Duration that defines how long fetched config data is available, in
    ///                            seconds. When the config data expires, a new fetch is required.
    /// @param completionHandler   Fetch operation callback.
    open func fetch(withExpirationDuration expirationDuration: TimeInterval, completionHandler: FirebaseRemoteConfig.FIRRemoteConfigFetchCompletion? = nil)

fetch(withExpirationDuration: interval)是超时获取数据,这是你的时间间隔。

let interval: TimeInterval = 10
Timer.scheduledTimer(timeInterval: interval,
                         target: self,
                         selector: #selector(updateConfig),
                         userInfo: nil,
                         repeats: true)

func updateConfig() {
    let rc = FIRRemoteConfig.remoteConfig()

    FIRRemoteConfig.remoteConfig().fetch { (status, error) in
        guard error == nil else {
        //handle error here
        return
        }

        FIRRemoteConfig.remoteConfig().activateFetched()
        let test = rc["key1"].stringValue //this runs only once
    }
}
于 2016-12-30T05:39:11.377 回答