Firebase Firestore 读取失败 SWIFT

问题描述 投票:0回答:1

我创建了一个按钮来查询我的 Firestore 数据库。我刚刚复制并粘贴了文档中的代码。

import UIKit
import Firebase
import FirebaseCore
import FirebaseFirestore

@objc func readButtonTapped() async {
    
    let docRef = self.database.collection("TestCollection").document("TestDocument")

    do {
      let document = try await docRef.getDocument()
      if document.exists {
        let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
        print("Document data: \(dataDescription)")
      } else {
        print("Document does not exist")
      }
    } catch {
      print("Error getting document: \(error)")
    }
    print("test read done")
}

错误消息发送至应用程序委托,内容为:

线程1:EXC_BAD_ACCESS(代码=1,地址=0x8)

项目运行并且“写入”功能起作用。应用程序委托已正确配置。按下按钮会导致上述错误消息出现在应用程序代理上。

请参阅下面的我的应用程序代理:

import UIKit
import FirebaseCore
import FirebaseFirestore

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    FirebaseApp.configure()

    let db = Firestore.firestore()
    
    return true
}
ios swift firebase asynchronous google-cloud-firestore
1个回答
0
投票
    let docRef = self.database.collection("TestCollection").document("TestDocument")

    docRef.getDocument { (document, error) in
        if let document = document, document.exists {
            let data = document.data()
            print(data)
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.