android - 无法访问 Firebase 节点以检索应用的频道列表
问题描述
一旦删除了与我的 Firebase 数据库中的通道相关的所有节点,我的代码应该更新列表,然后 notifyDataSetChanged() 到列表适配器,除非在删除 /channellist 下的通道节点后没有被删除,我的后续/channellist 的子侦听器无法访问 ref.child("/channellist/$channelname") 下的数据。所以由于某种原因我无法访问/channellist。
您可以看到 /channel 节点下没有 channel2,因为它已被删除,并且由于某种原因 channellist 下的 channel2 没有。
这是我的删除和列表更新功能的代码:
fun deleteChannel() {
Log.d("channeltodelete", channelToDelete)
val messageKeysToDelete = ArrayList<String>()
fb.child("/channel/$channelToDelete/messages")
.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(data: DataSnapshot) {
if (!data.exists()) {
deleteChannelHelper(channelToDelete)
fb.child("/channel/$channelToDelete").removeValue()
return
}
// Deletes the messages under /message
val messagemap = data.getValue(typeindicator2)!!
for (key in messagemap) {
messageKeysToDelete.add(key.key)
}
for (key in messageKeysToDelete) {
fb.child("message/$key").removeValue()
}
// DeleteChannelHelper deletes under user/channels
deleteChannelHelper(channelToDelete)
var fb2 = FirebaseDatabase.getInstance().reference
fb.child("/channel/$channelToDelete").removeValue()
**// This is the line of code that doesn't work
fb2.child("/channellist/$channelToDelete").removeValue()**
}
override fun onCancelled(databaseError: DatabaseError) {
// report/log the error
}
}
fun processChannelListData(data: DataSnapshot) {
Log.d("channellistlistener", data.key + ": " + data.value)
val channelnames = data.children.toMutableList()
var channels = ArrayList<String>()
var channelsToDelete = ArrayList<String>()
for (channel in channelnames) {
channels.add(channel.key.toString())
}
Log.d("channelnameslistener", channels.toString())
// If a channel was added
for (channel in channels) {
if (channel !in channelList) {
channelList.add(channel)
}
}
// If a channel was deleted
for (channel in channelList) {
if (channel !in channels) {
channelsToDelete.add(channel)
}
}
channelList.removeAll(channelsToDelete)
Log.d("chanList removeall", channelList.toString())
myAdapter.notifyDataSetChanged()
}
我注意到的是,放置在 /channellist 的 ChildEventListener 不仅在 /channellist 节点上触发了多次,而且出于某种原因对所有子节点也触发了一次,并且它弄乱了全局 channelList,因为如果最后访问的节点是 test1,看起来像
2020-09-14 16:26:18.734 24448-24523/com.example.treechat D/channellistlistener: test1: -MGyjrKRLmc-wPLAjvR-
2020-09-14 16:26:18.734 24448-24523/com.example.treechat D/channelnameslistener: []
...
2020-09-14 16:26:18.734 24448-24523/com.example.treechat D/chanList removeall: []
而如果它只是顶级节点的代码就可以了
2020-09-14 16:26:18.733 24448-24520/com.example.treechat D/channellistlistener: channellist: {channel1=-MHCK1wp8mBUi_bJzxZ-, channel2=-MHDAM_k2no7ZLtUYTIU, test1=-MGyjrKRLmc-wPLAjvR-}
2020-09-14 16:26:18.733 24448-24520/com.example.treechat D/channelnameslistener: [channel1, channel2, test1]
...
2020-09-14 16:26:18.733 24448-24520/com.example.treechat D/chanList removeall: [channel1, channel2, test1]
顺便说一句,我在创建频道时访问 /channellist 没有问题,因为您可以看到节点创建得很好。但是由于某种原因,当我这样做时应用程序崩溃了,而不是从 ChannelListActivity -> ChannelActivity,它来自 ChannelListActivity -> ChannelActivity -> mini-crash -> ChannelListActivity。我将在一个单独的问题中发布,但没有致命的异常日志。
解决方案
我恢复为 /channellist 使用 ValueEventListener 而不是 ChildEventListener。
我发现使用 HashMap<String, Any?>() 并将路径为 null 作为我要删除的所有位置的值可以一次删除所有节点,包括在通道列表中。获得 HashMap 后,您需要在数据库引用上调用 updateChildren(HashMap)。
推荐阅读
- python - 如何在 html 页面上显示 tqdm 进度?
- google-chrome-extension - 向 Chrome OAuth 身份屏幕添加图标?
- sql - 当我尝试在 IBM Db2 云上加载数据时,弹出一个无效的身份验证或令牌过期,有人知道如何解决这个问题吗?
- azure - 如何创建使用二头肌而不是 JSON 模板的“部署到 Azure”按钮?
- javascript - 计时器计数太快
- typescript - 类型推断、柯里化和泛型未返回预期类型
- python - Selenium 键盘。仅将键发送到特定窗口
- aws-lambda - aws xgboost - lambda 函数
- wordpress - WP_Query 或 get_posts() 崩溃函数
- python - 如何解决处理矩阵并将值与第二个矩阵组合的问题?