web-dev-qa-db-ja.com

SWIFT / IOS - FireBaseリモート設定フェッチ値が完了しません

以下は、FireBase Consoleからリモート設定値を取得するために実装されたクラスですが、Fetch Cloud値を呼び出すと、完了ブロックは決して実行されません(すなわち、完了ブロックで壊れている場合はXcodeは決して壊れません)。すべてのドキュメントが今古くなっているようです、そして私は私が間違っていることを見ることができません。

@objc class RemoteConfigValues: NSObject {

    @objc static let sharedInstance = RemoteConfigValues()

    private override init() {
        super.init()
        let settings = RemoteConfigSettings()

        //WARNING THIS IS FOR DEBUG ONLY
        settings.minimumFetchInterval = 0
        RemoteConfig.remoteConfig().configSettings = settings
        loadDefaultValues()
        fetchCloudValuesWith(fetchInterval: 0.0)
    }

    func loadDefaultValues() {
        let appDefaults: [String: Any?] = [
            RemoteConfigKeys.senseAdType.rawValue: String("A"),
            RemoteConfigKeys.sensePromotionButton.rawValue: String("BUY NOW")
        ]
        RemoteConfig.remoteConfig().setDefaults(appDefaults as? [String: NSObject])
    }

    func getInstanceIDToken() {
        InstanceID.instanceID().instanceID { (result, error) in
          if let error = error {
            print("Error fetching remote instance ID: \(error)")
          } else if let result = result {
            print("Remote instance ID token: \(result.token)")
          }
        }
    }

    func fetchCloudValues() {
        RemoteConfig.remoteConfig().fetchAndActivate { (status, error) in
            if let error = error {
                print("Uh-oh. Got an error fetching remote values \(error)")
                return
            }
            print("Retrieved values from the cloud!")
        }
    }

    func fetchCloudValuesWith(fetchInterval: Double) {
        RemoteConfig.remoteConfig().fetch(withExpirationDuration: fetchInterval) { status, error in

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

            RemoteConfig.remoteConfig().activate { (error) in
                print("Uh-oh. Got an error fetching remote values \(error?.localizedDescription ?? "")")
            }
            print("Retrieved values from the cloud!")
        }
    }

}
 _

以下は、FireBase/RemoteConfigのコンソールに印刷されているログです。

2020-06-11 10:16:20.061816+0100 MyApp[67337:10404626] 6.12.0 - [Firebase/RemoteConfig][I-RCN000062] Loading database at path /Users/dominicbryan/Library/Developer/CoreSimulator/Devices/481BF064-0BC7-404E-836F-A0AB58FD8900/data/Containers/Data/Application/77B9DD80-FFF4-4354-8B30-23E39C794861/Library/Application Support/Google/RemoteConfig/RemoteConfig.sqlite3
2020-06-11 10:16:20.064711+0100 MyApp[67337:10404626] 6.12.0 - [Firebase/RemoteConfig]2020-06-11 10:16:20.065426+0100 MyApp[67337:10404579] <NSProgress: 0x600000f5c5a0> : Parent: 0x0 (portion: 0) / Fraction completed: 1.0000 / Completed: 2203 of 2203
2020-06-11 10:16:20.066622+0100 MyApp[67337:10404626] 6.12.0 - [Firebase/RemoteConfig][I-RCN000039] Starting requesting token.
2020-06-11 10:16:21.164733+0100 MyApp[67337:10404579] 6.12.0 - [Firebase/RemoteConfig][I-RCN000022] Success to get iid : fqcuK-XSZsU.
2020-06-11 10:16:21.166088+0100 MyApp[67337:10404579] 6.12.0 - [Firebase/RemoteConfig][I-RCN000024] Success to get device authentication ID: 5**************7, security token: 7***************8.
2020-06-11 10:16:21.166737+0100 MyApp[67337:10404579] 6.12.0 - [Firebase/RemoteConfig][I-RCN000060] Fetch with user properties completed.
 _
6
Dom Bryan

fetchAndActivateを使用する代わりにfunc fetchCloudValuesWith(fetchInterval: Double)を使用してください。

remoteConfig.fetch(withExpirationDuration: 0) { [weak self] (status, error) in
    //Do your stuffs    
}
 _
1
developervtph

RemoteConfig.remoteConfig()を使用して直接RemoteConfigのシングルトン変数を作成します。

公式のFirebaseの文書で述べたように、 ここ

次の例に示すように、Singleton Remote Configオブジェクトを作成します。

これがそのためのコードです。

private var remoteConfig : RemoteConfig!

override func viewDidLoad() {
    super.viewDidLoad()
    remoteConfig = RemoteConfig.remoteConfig()
    let settings = RemoteConfigSettings()
    settings.minimumFetchInterval = 0
    remoteConfig.configSettings = settings

    NotificationCenter.default.addObserver(self, selector: #selector(fg), name: UIApplication.willEnterForegroundNotification, object: nil)

    // Do any additional setup after loading the view.
}

 override func viewDidAppear(_ animated: Bool) {
    print(#function)
    fetchData()
}
func fetchData() {

    remoteConfig.fetch { (status, error) in

        if status == .success {
            print("Config fetched!")
            self.remoteConfig.activate() { (error) in
                // ...
            }
        } else {
            print("Config not fetched")
            print("Error: \(error?.localizedDescription ?? "No error available.")")
        }
        self.doSomething()
    }
}

func doSomething() {
    key1 = remoteConfig[key1].stringValue
    key2 = remoteConfig[key2].boolValue  }
 _

お役に立てれば。

0
khush