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()
}
}
}
我添加了一个侦听器,但无法弄清楚如何断开/删除它。谢谢!
答案 0 :(得分:2)
如detaching a listener上的文档所示,您需要保留从addSnapshotListener
返回的值:
var listener = FriendSystem.system.USER_REF.addSnapshotListener { snapshot, error in
...
然后,您可以使用以下方法删除侦听器:
listener.remove()