如何删除SnapShot侦听器(Firestore-Swift)

时间:2020-01-04 12:27:35

标签: swift firebase google-cloud-firestore

func addUserObserver(_ update: @escaping () -> Void) {
    FriendSystem.system.USER_REF.addSnapshotListener { snapshot, error in
        self.userList.removeAll()

        guard error == nil else {
            print("Error retreiving collection")
            return
        }
        for document in snapshot!.documents {
            let email =  document.get("email") as! String
            if email != Auth.auth().currentUser?.email! {
                self.userList.append(User(userEmail: email, userID: document.documentID))
            }
            update()
        }
    }
}

我添加了一个侦听器,但无法弄清楚如何断开/删除它。谢谢!

1 个答案:

答案 0 :(得分:2)

detaching a listener上的文档所示,您需要保留从addSnapshotListener返回的值:

var listener = FriendSystem.system.USER_REF.addSnapshotListener { snapshot, error in
    ...

然后,您可以使用以下方法删除侦听器:

listener.remove()