【问题标题】:Swift/iOS - Firebase Remote Config fetch values never completesSwift/iOS - Firebase 远程配置获取值永远不会完成
【发布时间】:2020-09-28 20:51:32
【问题描述】:

下面是一个实现从 Firebase 控制台获取远程配置值的类,但是当调用获取云值时,完成块永远不会执行(即,如果我在完成块中中断,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.

【问题讨论】:

  • 我的回答是否帮助您解决了问题?如果是这样,请接受它,以便其他人也从中受益。
  • 我有同样的问题,我在 AppDelegate 中有 FirebaseApp.configure()。在另一个视图控制器上,我调用了 remoteConfig.fetch() 并且回调从未触发!

标签: ios swift firebase firebase-remote-config


【解决方案1】:

使用您的func fetchCloudValuesWith(fetchInterval: Double) 而不是使用fetchAndActivate

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

【讨论】:

  • 我已更新我的答案以表明我正在使用 fetchCloudValuesWith(fetchInterval: Double)。但是再次完成永远不会被调用
【解决方案2】:

任何人来这里寻找答案,我发现 Firebase pod 在我的主项目以及我创建的模块之一中被用作动态库。这复制了 Firebase 逻辑并导致它什么也不做。

【讨论】:

  • 和我一样。应该注意的是,有问题的 pod 是 FirebaseAnalytics。有一些 Firebase pod 是静态的。
【解决方案3】:

RemoteConfig 创建一个单例变量,而不是直接使用RemoteConfig.remoteConfig()

正如官方firebase文档中提到的here

创建单例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  }

希望这会有所帮助。

【讨论】:

  • 不幸的是,这没有任何区别。您只是在创建一个指向同一个 RemoteConfig 引用的新指针。
  • 我粘贴的代码对我有用。所以想发给你。 @d
  • 好的,谢谢你的回答,很遗憾我不会选择它作为我的问题的答案
猜你喜欢
  • 1970-01-01
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多