Search code examples
iosswiftfirebasefirebase-remote-config

Firebase Remote Config returns 0 (Swift iOS)


I have added Firebase Remote Config to my app. It successfully fetches the config but always returns 0. The code I am using is below. It is currently in viewDidLoad if that makes a difference?

Thank you

let remoteConfig = FIRRemoteConfig.remoteConfig()
let iTry = remoteConfig["iTryAgainSessions"].numberValue!
let tryInt = Int(iTry)
print("I Try: \(tryInt)")

Solution

  • There are a few of things you need to get Remote Config up and running.

    1. Set default values: You can set default values that will be used in the event that values have not yet been fetched from the server. Default values can be set using the FIRRemoteConfig setDefaults method. If no defaults are set then initial static values will be returned. In your case that is why you keep getting 0 for the numberValue since the initial static value for number is 0.

      // Set default values from plist file.
      self.remoteConfig = [FIRRemoteConfig remoteConfig];
      [self.remoteConfig setDefaultsFromPlistFileName:@"RemoteConfigDefaults"];
      
    2. Fetch remote values: You can call FIRRemoteConfig fetch method to retrieve the values set on the server.

    3. Apply fetched values: After a successful fetch you MUST call FIRRemoteConfig activateFetched method to apply the values retrieved from the server. This is to prevent your app from suddenly changing when new values are fetched, so the activateFetched method gives you the control over when those values are used. Feel free to call activateFetched as soon as the fetch returns if that is what you want.

    4. Note that you should enable developer mode so that you are given more opportunity to fetch config values from the server, in a production app there is a limit of 5 requests per hour, after that the cached values will be used for the remainder of the hour.

    Try a sample that demonstrates the above here.