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

React Native中Firestore调用setDoc()后getDoc()无限挂起问题求助

React Native Firestore:setDoc后getDoc无限挂起问题

我在React Native应用中使用@react-native-firebase/firestore时遇到一个诡异问题:对同一文档调用setDoc()后,后续的getDoc()会无限挂起。这两个调用并非在同一位置顺序执行,它们是应用不同部分调用的导出函数,甚至两者之间可能间隔数分钟。

简化后的示例代码如下:

更新文档函数

import { getFirestore, doc, getDoc, setDoc } from '@react-native-firebase/firestore';
  
async updateBlock(nivel: string, unidad: string, bloque: string, exercises: AppExercise[]): Promise<void> {
    const key = docId(nivel, unidad, bloque);
    await setDoc(doc(getFirestore(), 'app_exercises', key), { exercises: exercisesMap });
},

获取文档函数

import { getFirestore, doc, getDoc, setDoc } from '@react-native-firebase/firestore';

async getBlock(nivel: string, unidad: string, bloque: string): Promise<AppExercise[]> {
    const key = docId(nivel, unidad, bloque);

    const snap = await getDoc(doc(getFirestore(), 'app_exercises', key));
    if (!snap.exists()) throw new Error(`[exerciseRepository] ${key} no encontrado en Firestore`);

    return result;
}

可能的原因及解决方向

  • 复用Firestore实例:每次调用getFirestore()会生成新实例,可能导致连接池异常。建议全局初始化一个Firestore实例,所有操作复用它。
  • 添加异常捕获:异步函数未处理的异常可能导致Firestore内部状态紊乱,给setDoc和getDoc加上try/catch,确保错误被捕获并处理。
  • 版本兼容性检查:确认@react-native-firebase/firestore与React Native、Firebase核心库的版本匹配,部分版本组合存在已知死锁问题。
  • 强制从服务器获取:调用getDoc时添加{ source: 'server' }选项,跳过本地缓存,避免缓存状态异常导致挂起。
  • 使用merge选项:给setDoc添加{ merge: true },避免全量覆盖文档时可能触发的隐性冲突。

调整后的示例代码

// 全局初始化Firestore实例(放在单独的配置文件中)
import { initializeApp } from '@react-native-firebase/app';
import { getFirestore } from '@react-native-firebase/firestore';

const app = initializeApp();
export const db = getFirestore(app);
// 更新文档函数
import { doc, setDoc } from '@react-native-firebase/firestore';
import { db } from './firebase-config'; // 导入全局实例

async updateBlock(nivel: string, unidad: string, bloque: string, exercises: AppExercise[]): Promise<void> {
  try {
    const key = docId(nivel, unidad, bloque);
    await setDoc(doc(db, 'app_exercises', key), { exercises: exercisesMap }, { merge: true });
  } catch (err) {
    console.error('更新练习块失败:', err);
    throw err;
  }
}
// 获取文档函数
import { doc, getDoc } from '@react-native-firebase/firestore';
import { db } from './firebase-config'; // 导入全局实例

async getBlock(nivel: string, unidad: string, bloque: string): Promise<AppExercise[]> {
  try {
    const key = docId(nivel, unidad, bloque);
    const docRef = doc(db, 'app_exercises', key);
    // 强制从服务器获取数据
    const snap = await getDoc(docRef, { source: 'server' });
    
    if (!snap.exists()) throw new Error(`[exerciseRepository] ${key} no encontrado en Firestore`);
    
    const data = snap.data();
    return data?.exercises as AppExercise[];
  } catch (err) {
    console.error('获取练习块失败:', err);
    throw err;
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.02 06:34:50