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

Angular/Fire docData在Capacitor-iOS无输出,桌面浏览器正常

Angular/Fire docData 在 Capacitor-iOS 中无输出的调试问题

Angular/Fire的docData方法在macOS的Chrome/Safari浏览器中可正常输出数据,但在Capacitor-iOS(基于WebKit-Safari)环境下始终无输出。

已确认以下信息:

  • Game对象存在,桌面浏览器可正常显示输出,Capacitor-iOS原生应用中也能与该对象交互,但无法接收流更新
  • gameDocRef日志能正确记录文档路径
  • 非流式的Firestore读写删操作在iOS上均正常
  • 网络日志显示:iOS调用docData后无任何网络活动,未创建流;而MacBook的Chrome中有带"channel"的网络请求

虽有自定义Capacitor Firestore插件,但该插件仅支持回调方式,无返回Observable的docData类方法,因此倾向使用Angular/Fire。


更新情况

使用Firestore Web模块化API自定义docData函数,在桌面Chrome/Safari正常工作,但Capacitor-iOS仍无法创建通道;直接调用onSnapshot也仅在桌面触发日志输出,iOS无反应。怀疑Firebase/Firestore Web SDK的onSnapshot方法存在Bug,求调试提示。


桌面正常运行代码

const gameDocRef = doc(this.firestore, 'game', game.id);
if (showGameLogs) {
  let toLog: any = (gameDocRef as any)._key;
  console.log("gameDocRef:", gameDocRef);
  console.log("🟩 CALLING DOC_DATA ON THIS GAME DOC REF:", toLog?.path?.segments);
}
const game$ = docData(gameDocRef, { idField: 'id' }).pipe(
  map(data => {
    if (showGameLogs) {
      console.log("🟩 docData EMISSION:", data);
    }
    if (data === undefined) {
      return null;
    } else {
      return data;
    }
  })
) as Observable<Game | null>;

自定义docData代码

import { onSnapshot } from "firebase/firestore";

function docData<T>(docRef: DocumentReference<T>, options?: { idField?: string }): Observable<T> {
  return new Observable<T>(observer => {
    const unsubscribe = onSnapshot(docRef,
      (docSnapshot) => {
        if (docSnapshot.exists()) {
          const data = docSnapshot.data() as T;
          if (options?.idField) {
            (data as any)[options.idField] = docSnapshot.id;
          }
          observer.next(data);
        } else {
          observer.next(undefined as any);
        }
      },
      (error) => {
        console.error("Error fetching document:", error);
        observer.error(error);
      }
    );
    return { unsubscribe };
  });
}

直接调用onSnapshot代码

onSnapshot(gameDocRef, () => console.log("EMISSION DETECTED"));

内容的提问来源于stack exchange,提问作者frontend-ux-ui

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 12:40:56