Swift Firebase:更新由Firebase查询产生的特定对象 [英] Swift Firebase: Update specific objects resulting from Firebase query

查看:28
本文介绍了Swift Firebase:更新由Firebase查询产生的特定对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Firebase数据库中具有以下结构:

I have the following structure in my Firebase DB:

我希望将具有特定 mealID 的所有对象的 read 属性更新为 true .我已经尝试过了:

I am looking to update the read attribute to true for all objects with a particular mealID. I have attempted this:

if let selectedIndexPath = tableView.indexPathForSelectedRow {
                meals[(selectedIndexPath as NSIndexPath).row].unreadNotification=false
                let mealId = mealIds[(selectedIndexPath as NSIndexPath).row]
                let notificationsRef = ref.child("notifications")
                //get all notifications with this mealId
                var mealNotifications = notificationsRef.queryOrdered(byChild: "mealId").queryEqual(toValue: mealId)
                //notificationRef.updateChildValues(["read":true])
            }

查询输出的是正确的通知对象,我只是不确定如何更新这些特定对象(使用 mealNotifications ).有什么想法吗?

The query is outputting the correct notification objects, I am just not sure of how to update these specific objects (using mealNotifications). Any ideas?

编辑

我添加了一个观察事件,但是我在输出中没有收到任何对象(我希望有一个输出).我相信这是因为我正在查看通知ID级别,而不是 mealID 级别.如何在查询中更正此问题?

I have added an observe event, however I am not receiving any objects in the output (I am expecting a single output). I believe this is because I am looking on the notification ID level, rather than the mealID level. How can I correct this in the query?

if let selectedIndexPath = tableView.indexPathForSelectedRow {
                meals[(selectedIndexPath as NSIndexPath).row].unreadNotification=false
                let mealId = mealIds[(selectedIndexPath as NSIndexPath).row]
                let notificationsRef = ref.child("notifications")
                //get all notifications with this mealId
                var mealNotifications = notificationsRef.queryOrdered(byChild: "mealId").queryEqual(toValue: mealId).observe(.value, with: {snapshot in
                    print("MEALS:",snapshot)
                    })
                //notificationRef.updateChildValues(["read":true])
            }

推荐答案

以下方法有效:

if let selectedIndexPath = tableView.indexPathForSelectedRow {
                meals[(selectedIndexPath as NSIndexPath).row].unreadNotification=false
                let mealId = mealIds[(selectedIndexPath as NSIndexPath).row]
                let notificationsRef = ref.child("notifications")
                //get all notifications with this mealId
                var mealNotifications = notificationsRef.queryOrdered(byChild: "mealID").queryEqual(toValue: mealId).observe(.value, with: {snapshot in
                    let enumerator = snapshot.children
                    while let rest = enumerator.nextObject() as? FIRDataSnapshot {
                        notificationsRef.child(rest.key).child("read").setValue(true)
                        }
                        })
                    tableView.reloadRows(at: [selectedIndexPath], with: .none)
                }

这篇关于Swift Firebase:更新由Firebase查询产生的特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆