You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何正确处理Firebase RemoteConfig获取的Pending状态?解决重复请求报错

Firebase RemoteConfig 重复请求错误的正确处理方案

问题本质

你遇到的核心问题是:当Fetch请求返回Pending(Invalid)失败状态后,立即重试FetchAsync,但Firebase RemoteConfig内部的前一次请求状态还未完全清理,导致触发「Duplicate request while the previous one is pending」错误。即使ContinueWith里判断任务已完成,Firebase内部仍可能存在未收尾的请求标记。

正确处理方式

1. 加请求锁避免并发

维护一个全局状态变量,标记当前是否正在执行Fetch请求,发起新请求前先检查该状态,防止重复调用。

2. 延迟重试+次数限制

不要在失败后立即重试,给系统留足清理前一次请求状态的时间;同时设置最大重试次数,避免无限循环重试。

3. 优先排查配置合法性

当LastFetchFailureReason为Invalid时,先检查Firebase控制台的RemoteConfig配置:比如参数类型是否正确、项目权限是否配置到位,排除配置本身的问题。

修正后的示例代码

private bool _isFetching = false;
private int _retryCount = 0;
// 可根据业务调整重试次数和延迟时间
private const int MaxRetryTimes = 3;
private const int RetryDelaySeconds = 2;

private async Task FetchRemoteConfigAsync()
{
    // 正在请求时直接返回,避免重复调用
    if (_isFetching) return;
    _isFetching = true;

    try
    {
        await FirebaseRemoteConfig.DefaultInstance.FetchAsync(TimeSpan.Zero);
        var remoteConfig = FirebaseRemoteConfig.DefaultInstance;
        var info = remoteConfig.Info;

        if (info.LastFetchStatus == LastFetchStatus.Success)
        {
            Debug.Log("FirebaseRemoteConfig -- Fetched Successfully");
            ActivateRemoteConfig();
            _retryCount = 0; // 成功后重置重试次数
        }
        else
        {
            CustomDebug.LogErrorFormat("FirebaseRemoteConfig - Fetch was unsuccessful:{0} reason:{1}", info.LastFetchStatus, info.LastFetchFailureReason);
            
            if (_retryCount < MaxRetryTimes)
            {
                _retryCount++;
                // 延迟后重试
                await Task.Delay(TimeSpan.FromSeconds(RetryDelaySeconds));
                await FetchRemoteConfigAsync();
            }
            else
            {
                CustomDebug.LogError("FirebaseRemoteConfig - 已达最大重试次数,请求失败");
            }
        }
    }
    catch (FirebaseException ex)
    {
        CustomDebug.LogErrorFormat("FirebaseRemoteConfig - 请求异常:{0}", ex.Message);
        if (_retryCount < MaxRetryTimes)
        {
            _retryCount++;
            await Task.Delay(TimeSpan.FromSeconds(RetryDelaySeconds));
            await FetchRemoteConfigAsync();
        }
    }
    finally
    {
        // 无论成功失败,都释放请求锁
        _isFetching = false;
    }
}

关键说明

  • _isFetching变量确保同一时间只有一个Fetch请求在执行,彻底避免重复请求错误。
  • 延迟重试给Firebase内部足够时间清理前一次请求的状态,解决Pending状态残留问题。
  • 最大重试次数防止因配置错误导致无限循环,避免占用过多系统资源。

内容的提问来源于stack exchange,提问作者docchang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 06:52:29