简体   繁体   English

在 Firestore 中收听一个添加的文档 - Swift

[英]Listening to one added document in Firestore - Swift

I'm listening to only one added document in a collection, and after it is read I need this document to be deleted.我只听一个集合中添加的文档,阅读后我需要删除该文档。 This is the code I implemented:这是我实现的代码:

func createListener(){
    guard let currentUid = Auth.auth().currentUser?.uid else { return }
    listener = db.collection("Collection").document(currentUid).collection("Collection").addSnapshotListener({ listenerSnapshot, error in
        if let error = error{
            print(error.localizedDescription)
            return
        }
        listenerSnapshot?.documentChanges.forEach({ change in
            if change.type == .added{
                let data = change.document.data()
                
                let myview = MYView(data: data)
                guard let window = UIApplication.shared.windows.last else { return }
                myview.present(to: window) {
                    change.document.reference.delete()
                }
            }
        })
    })
}

The problem is that after the document is deleted with问题是文件被删除后

change.document.reference.delete()更改.document.reference.delete()

The listener snippet change.type ==.added is triggered even if the document has been deleted.即使文档已被删除,侦听器片段change.type ==.added也会被触发。 I don't know why...我不知道为什么...

How can I only listen for actually ADDED documents in a Firestore Collection?我如何才能只收听 Firestore 集合中实际添加的文档?

EDIT:编辑:

listening for a specific document but still closure called when document is deleted:侦听特定文档但在删除文档时仍调用闭包:

listener = db.collection("Collection").document(currentUid).addSnapshotListener({ listenerSnapshot, error in
        if let error = error{
            print(error.localizedDescription)
            return
        }
        guard let data = listenerSnapshot?.data() else { return }
        let myview = MYView(data: data)
            
        guard let window = UIApplication.shared.windows.last else { return }
        myview.present(to: window) {
            listenerSnapshot?.reference.delete()
        }
    })

I'm listening to only one added document in a collection.我只听了一个集合中添加的文档。

No, you're not.不你不是。 You're attaching a snapshot listener to a collection and not to a single document:您将快照侦听器附加到集合而不是单个文档:

db.collection("Collection")
  .document(currentUid)
  .collection("Collection") //👈
  .addSnapshotListener(/* ... */)

This means that you're listening for real-time updates for each operation that takes place inside the entire sub-collection called "Collection".这意味着您正在侦听在称为“集合”的整个子集合中发生的每个操作的实时更新。

What you're basically doing, you're saying, hey Firestore, give me all documents that exist in that sub-collection and keep the listener alive.你基本上在做什么,你在说,嘿 Firestore,给我那个子集合中存在的所有文档,让听众保持活力。 This listener will be invoked every time a document in that collection changes over time.每当该集合中的文档随时间发生变化时,都会调用此侦听器。

If you want to listen to a single document, then you should add a call to .document() and pass a specific document ID:如果您想收听单个文档,则应添加对.document()的调用并传递特定的文档 ID:

db.collection("Collection")
  .document(currentUid)
  .collection("Collection")
  .document("someDocumentId") //👈
  .addSnapshotListener(/* ... */)

In this way, you'll only be notified about the changes to a single document.这样,您只会收到有关单个文档更改的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM