0

I'm using Firebase Remote Config, I have some troubles to update values. My values are updated only if I close and relaunch the app.

But never if my app enters in foreground.

The developer is activated, with no cache delay.

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        let _ = RCFirebaseValues.sharedInstance
    }
}

My Firebase Remote Config class:

enum ValueKey: String {
    case force_update
}

class RCFirebaseValues {

    static let sharedInstance = RCFirebaseValues()
    var loadingDoneCallback: (() -> ())?
    var fetchComplete: Bool = false

    private init() {    
        loadDefaultValues()
        fetchCloudValues()
    }

    func loadDefaultValues() {
       RemoteConfig.remoteConfig().setDefaults(fromPlist: "RemoteConfigDefaults")
    }

    func fetchCloudValues() {

        #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 {
                DLog(message:"Uh-oh. Got an error fetching remote values \(String(describing: error))")
                return
            }

            RemoteConfig.remoteConfig().activateFetched()

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


    func bool(forKey key: ValueKey) -> Bool {
        return RemoteConfig.remoteConfig()[key.rawValue].boolValue
    }

    func string(forKey key: ValueKey) -> String {
        return RemoteConfig.remoteConfig()[key.rawValue].stringValue ?? ""
    }

    func double(forKey key: ValueKey) -> Double {
        if let numberValue = RemoteConfig.remoteConfig()[key.rawValue].numberValue {
            return numberValue.doubleValue
        } else {
            return 0.0
        }
    }
}

What's wrong?

EDIT after Mosbah's response:

class AppDelegate: UIResponder, UIApplicationDelegate {

     var remoteConfig:RCFirebaseValues!

     func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         FirebaseApp.configure()
         self. remoteConfig = RCFirebaseValues.sharedInstance
      }

}

cmii
  • 3,556
  • 8
  • 38
  • 69

1 Answers1

-1

Your RCFirebaseValues scope is wrong, it will be nil as soon as you are out of application: didFinishLaunchingWithOptions: so you should keep a strong reference to your object (create a var on AppDelegate).

Mosbah
  • 1,347
  • 1
  • 14
  • 28
  • This: `self.firebaseRC = RCFirebaseValues.sharedInstance`? I tried, but I have the same result, values are not upated :( – cmii Jan 21 '19 at 14:01
  • yes of course, I added var reference to app delegate – cmii Jan 21 '19 at 14:34