简体   繁体   English

无法取消订阅 Firebase 中的侦听器

[英]Can't unsubscribe from Listener in Firebase

I have a Listener set up in my Firebase Realtime Database that notices changes to data of the users friends.我在 Firebase 实时数据库中设置了一个监听器,用于通知用户朋友的数据更改。 But when the user removes one of the friends from his friend list I want to unsubscribe from these data changes.但是当用户从他的朋友列表中删除一位朋友时,我想取消订阅这些数据更改。 I tried doing so like in the following:我尝试这样做,如下所示:

databaseReference.Child("usernames").Child(removedFriendName)
.ChildChanged -= HandleChildChangedFriend;

The reference and path to the database are exactly the same as they were when I subscribed to changes of the data.数据库的引用和路径与我订阅数据更改时完全相同。 However, after a user removes someone from his friendlist he still receives updates from this EventListener.然而,在用户从他的好友列表中删除某人之后,他仍然会收到来自这个 EventListener 的更新。

Hope someone can help me with this :)希望有人能帮我解决这个问题:)

Okay I finally found a solution to make my code work, yet I still don't understand why it didnt work before.好吧,我终于找到了使我的代码工作的解决方案,但我仍然不明白为什么它以前不起作用。 Doesn't matter I guess :)我猜没关系:)

The one thing I changed was that I saved the databaseReference inside a variable at the moment I added the EventListener and then later on used this variable again to remove the listener.我改变的一件事是我在添加 EventListener 时将 databaseReference 保存在一个变量中,然后再次使用这个变量来删除侦听器。

DatabaseReference friendReference = databaseReference.Child("usernames").Child(friendName);

friendReference.ChildChanged += HandleChildChangedFriend;

//Later 

friendReference.ChildChanged -= HandleChildChangedFriend;

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

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