首页 > 解决方案 > 子集合的 Firestore 监听器

问题描述

我通过以下方式设置了 Firestore:

Channels [collection] ----> channelID ---> Messages [collection] ---> messageID

如何将 snapshotListener 添加到子集合“消息”?

  Firestore.firestore().collection("Channels").document().collection("Messages").addSnapshotListener { (querySnapshot, error) in
        guard let snapshot = querySnapshot else {
            print("Error listening for channel updates: \(error?.localizedDescription ?? "No error")")
            return
        }

        snapshot.documentChanges.forEach { change in 
           print(change)
        }
    }

这对我不起作用

标签: iosswiftfirebasegoogle-cloud-firestore

解决方案


您不能让单个侦听器接收来自未知数量的子集合的更新。集合上的侦听器没有“通配符”运算符。您必须选择一个特定的集合或查询并为其附加一个侦听器。


推荐阅读