how to get integer value from Firebase Remote Config in iOS swift?
Asked Answered
B

3

2

so I set Firebase remote config default in my iOS like this:

let remoteConfig = RemoteConfig.remoteConfig()

// set remote config default value
let defaultRemoteConfig : [String:NSObject] = [
  "number_of_recommended_events_to_show_per_page" : 15 as NSObject
]

remoteConfig.setDefaults(defaultRemoteConfig)

 // Activate and refetch remote config data. 
 // I use 'Load Value for next time' loading strategy
 remoteConfig.activate()
 remoteConfig.fetch()

and then I want to get the value from remote like this

 // get the value from remote config
let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue as! Int

I need the value in Integer format, but it crash when I cast it to Int like that enter image description here

here is how I set the value in the console enter image description here

why is it nil ? how to fix this ?

Bonaire answered 21/2, 2020 at 9:40 Comment(4)
Did you fetchAndActivate your config before trying to access a value? And don't use force unwrap ! operator.Shun
@Shun yes, I fetch it before trying to get the value. I have edited my question. yup, it is better to avoid exclamation mark but just to make my problem clearerBonaire
Probably the rempote config has not been fetched yet... As far as I remember it's an asynch procedure. Use the fetch with completionHandler.Noelianoell
@Alexa289: you are welcome! I used the remote config more than one year ago for a big app I've worked on and I had the same problem. I do not remember the logic of the default values sorry. The crash is because you unwrapped the Optional without the control... NEVER do that :)Noelianoell
M
1

Make sure you've fetched in this block before getting a remote value.

func fetchCloudValues() {
  // WARNING: Don't actually do this in production!
  let fetchDuration: TimeInterval = 0

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

    if let error = error {
      print ("Uh-oh. Got an error fetching remote values \(error)")
      return
    }

    RemoteConfig.remoteConfig().activateFetched()
    print ("Retrieved values from the cloud!")
    let numberOfEvents = RemoteConfig.remoteConfig()
                                            .configValue(forKey: "number_of_recommended_events_to_show_per_page")
                                            .intValue ?? 0
    print("Our app's number of events is \(numberOfEvents)")

  }
}
Mcvay answered 21/2, 2020 at 11:33 Comment(0)
A
3

try this!

let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue?.intValue ?? 0
Anvil answered 21/2, 2020 at 10:34 Comment(0)
M
1

Make sure you've fetched in this block before getting a remote value.

func fetchCloudValues() {
  // WARNING: Don't actually do this in production!
  let fetchDuration: TimeInterval = 0

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

    if let error = error {
      print ("Uh-oh. Got an error fetching remote values \(error)")
      return
    }

    RemoteConfig.remoteConfig().activateFetched()
    print ("Retrieved values from the cloud!")
    let numberOfEvents = RemoteConfig.remoteConfig()
                                            .configValue(forKey: "number_of_recommended_events_to_show_per_page")
                                            .intValue ?? 0
    print("Our app's number of events is \(numberOfEvents)")

  }
}
Mcvay answered 21/2, 2020 at 11:33 Comment(0)
M
-1

Other answers didn't quite work for me. However the following did...

let numberOfDocumentsPerQuery = remoteConfig.configValue(forKey: "number_of_recommended_events_to_show_per_page").numberValue.intValue
Minstrel answered 3/8, 2022 at 19:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.