How to add timeout on Firebase RemoteConfig fetch request?
Asked Answered
L

2

6

I am using Firebase Remote Config feature in my app. I need to add a network timeout on the fetch request.

#if DEBUG
let expirationDuration: TimeInterval = 0
RemoteConfig.remoteConfig().configSettings = RemoteConfigSettings(developerModeEnabled: true)
#else
let expirationDuration: TimeInterval = 3600
#endif

RemoteConfig.remoteConfig().fetch(withExpirationDuration: expirationDuration) {
    [weak self] (status, error) in

    guard error == nil else {
        print ("Uh-oh. Got an error fetching remote values \(String(describing: error))")
            return
        }

    RemoteConfig.remoteConfig().activateFetched()

    self?.fetchComplete = true
    self?.loadingDoneCallback?()
}

How can I do?

Leonoraleonore answered 7/6, 2018 at 15:7 Comment(6)
Why do you need a timeout?Propman
@DougStevenson With a very bad network, my app loads indefinitely and never returns fetchComplete=trueLeonoraleonore
Your app shouldn't be waiting on the results of the fetch. You should handle the results asynchronously whenever it's finished.Propman
My parameters have to be load before launching my home screen.Leonoraleonore
I think you're going to have to implement something on your own. The Firebase APIs won't help you here.Propman
@DougStevenson ok thanks, do you have an idea to do that?Leonoraleonore
L
6

There's a fetchTimeout setting that you can use when you initialise FIRRemoteConfig.configSettings.

The documentation and details are here

Larva answered 2/1, 2020 at 7:52 Comment(0)
J
5

You can have a workaround for that by using a timer:

  remoteConfigTimer = Timer.scheduledTimer(withTimeInterval: 10,
                                                 repeats: false) {
                                                    timer in
                                                    self.remoteConfigTimerExpired = true
                                                    self.loadingDoneCallback()
  }

Here you define your timeout as the timeInterval of your Timer, and once this timer is reached you just execute the same method you would execute if the RemoteConfig call was successful.

Note that this is only available for iOS 10 or higher.

Janeyjangle answered 27/7, 2018 at 9:43 Comment(1)
Nice solution but I would have added remoteConfigTimer .invalidate() in the remote config fetch callback to prevent the timer from firing an error when the fetch was successful.Albie

© 2022 - 2024 — McMap. All rights reserved.