首页 > 解决方案 > 从 Firestore 检索的空数组中检索“getDocuments”查询中的数据

问题描述

我正在使用“getDocuments”查询从我的 Firestore 数据库中检索数据,但是我正在检索一个空数组。通过调试,我使用了a print(snapshot?.documents),发现返回了一个空数组。有人可以帮助我为什么要返回一个空数组吗?

获取通知功能:

    public func getNotifications(
    completion: @escaping ([IGNotification]) -> Void
) {
    guard let username = UserDefaults.standard.string(forKey: "username") else {
        completion([])
        return
    }
    let ref = firestoreDatabase.collection("users").document(username).collection("notifications")
    ref.getDocuments { snapshot, error in
        guard let notifications = snapshot?.documents.compactMap({
            IGNotification(with: $0.data())
        }),
        error == nil else {
            completion([])
            return
        }
        completion(notifications)
    }
}

Firestore 数据:

Firestore 数据的屏幕截图

标签: arraysxcodegoogle-cloud-firestoreswiftui

解决方案


推荐阅读