0

I have the following code that loads in the initial ViewController viewDidLoad. It works fine initially. But shouldn't it look for changes every 10 seconds?

When I make an update to a config value in Firebase and publish, I don't see this come through in the app. I am running in debug mode so throttling isn't an issue.

If I restart the app, I see the new value. Since the interval is set to 10 seconds, shouldn't I see the update while the app is running?

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
    }

Any ideas why this isn't update?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
4thSpace
  • 43,672
  • 97
  • 296
  • 475

1 Answers1

1

You should use scheduledTimer instead.

/// 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) is to fetch data with a timeout, that is your 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
    }
}
Willjay
  • 6,381
  • 4
  • 33
  • 58
  • "expirationDuration. Duration that defines how long fetched config data is available, in seconds". expirationDuration is how long its caches the fetched data. It is not a network timeout. – user3296487 Jul 27 '17 at 03:58