无法在SwiftUI中将对象添加到Mac应用程序中的Firestore

vbkedwbf  于 5个月前  发布在  Swift
关注(0)|答案(2)|浏览(44)

我从MacOS应用程序(SwiftUI)访问Firebase Firestore时遇到问题。所有要求都由我的应用程序提供,例如:

  • plist(bundle id,Firestore url等是正确的)
  • keychain access
  • 传入和传出连接

但是当我尝试setValue到我的Firestore数据库时,我仍然收到这样的错误:
10.19.0 - [Firebase Database][I-RDB 034005]服务器强制终止了Firebase数据库连接。将不会尝试重新连接。原因:Firebase错误。请确保正确配置了Firebase实时数据库示例的URL。
添加一个简单值的代码如下所示:

Database.database().reference().child("test").setValue(["test": 123])

字符串
启动Firebase的方法位于AppDelegate中:

final class AppDelegate: NSObject,  NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions" : true])
        FirebaseApp.configure()
    }
}


你知道这里缺少了什么吗

xlpyo6sf

xlpyo6sf1#

代码:

Database.database().reference().child("test").setValue(["test": 123])

字符串
是Firebase实时数据库(RTDB),而不是Firestore。
错误消息可能是由于您没有初始化项目中的RTDB服务.但如果您试图使用Firestore,请不要使用RTDB代码或其服务。

oyt4ldly

oyt4ldly2#

首先,你使用的代码不是firestore,而是firebase实时数据库。
要解决错误,请尝试此代码用于初始化DB

import Firebase
import FirebaseFirestore

let db = Firestore.firestore()

var ref: DocumentReference? = nil
ref = db.collection("test").addDocument(data: [
    "test": 123
]) { err in
    if let err = err {
        print("Error adding document: \(err)")
    } else {
        print("Document added with ID: \(ref!.documentID)")
    }
}

字符串
希望能帮上忙。

相关问题