【问题标题】:How to add timeout on Firebase RemoteConfig fetch request?如何在 Firebase RemoteConfig 获取请求上添加超时?
【发布时间】:2018-06-07 15:07:00
【问题描述】:

我在我的应用中使用 Firebase 远程配置功能。 我需要在获取请求上添加网络超时。

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

    RemoteConfig.remoteConfig().activateFetched()

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

我该怎么办?

【问题讨论】:

  • 为什么需要超时?
  • @DougStevenson 由于网络非常糟糕,我的应用程序无限期加载并且永远不会返回 fetchComplete=true
  • 您的应用不应等待获取结果。您应该在结果完成时异步处理结果。
  • 我的参数必须在启动主屏幕之前加载。
  • 我认为你必须自己实现一些东西。 Firebase API 在这里帮不了您。

标签: ios swift firebase timeout firebase-remote-config


【解决方案1】:

您可以通过使用计时器来解决此问题:

  remoteConfigTimer = Timer.scheduledTimer(withTimeInterval: 10,
                                                 repeats: false) {
                                                    timer in
                                                    self.remoteConfigTimerExpired = true
                                                    self.loadingDoneCallback()
  }

在这里,您将超时定义为 Timer 的 timeInterval,一旦达到此计时器,您只需执行与 RemoteConfig 调用成功时相同的方法。

请注意,这仅适用于 iOS 10 或更高版本。

【讨论】:

  • 不错的解决方案,但我会在远程配置获取回调中添加remoteConfigTimer .invalidate(),以防止在获取成功时计时器触发错误。
【解决方案2】:

您可以在初始化FIRRemoteConfig.configSettings 时使用fetchTimeout 设置。

文档和详细信息是here

【讨论】:

    猜你喜欢
    • 2020-11-28
    • 2022-11-19
    • 1970-01-01
    • 2022-01-11
    • 2020-05-21
    • 1970-01-01
    • 2017-11-07
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多