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

Firestore及Firestore模拟器连接异常问题求助

Firestore连接异常问题排查(模拟器/线上环境均失败)

测试用安全规则(本地与线上一致)

rules_version = "2";
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
     
      allow read, write: if true;
    }
  }
}

监听/写入代码实现

func subscribe() {
      if listenerRegistration == nil {
        listenerRegistration = db.collection("units").addSnapshotListener { (querySnapshot, error) in
          guard let documents = querySnapshot?.documents else {
            print("No documents")
            return
          }

          let units = documents.compactMap { queryDocumentSnapshot in
            try? queryDocumentSnapshot.data(as: UnitModel.self)
          }
        }
      }
    }

    private func addUnit(_ unit: UnitModel) {
        do {
          let _ = try db.collection("units").addDocument(from: unit)
        }
        catch let error {
            print("Firebase error \(error.localizedDescription)")
        }
      }

控制台错误日志(catch块从未触发)

10.22.1 - [FirebaseFirestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: An internal error has occurred, print and inspect the error details for more information. This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to

或

10.22.1 - [FirebaseFirestore][I-FST000001] WatchStream (104e08808) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.'

其他Firebase服务运行正常情况

  • Firebase Authentication在模拟器和线上环境均可正常工作,模拟器配置代码如下:
enum Development {
        static let kFirebaseAuthEmulatorPort = 9099
        static let kFirebaseFirestoreEmulatorPort = 8080
        static let kMachineLocalIP = "192.168.0.10"
    }


Auth.auth().useEmulator(withHost: Constants.Development.kMachineLocalIP,
                                port: Constants.Development.kFirebaseAuthEmulatorPort)
  • Firebase实时数据库也可正常使用,仅Firestore出现连接问题。即使Firestore使用Firestore.firestore().useEmulator(...)替换对应常量配置后,问题依旧存在。

提问

请问该问题的原因是什么?是我在读写/监听Firestore的方式上存在错误吗?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 17:50:15