如何使用childByAutoId更新Firebase中的值? [英] How to update value in Firebase with childByAutoId?

查看:224
本文介绍了如何使用childByAutoId更新Firebase中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Firebase中创建对象时,我使用childByAutoId。我以后如何更新这些特定的对象?我无法获取关键Firebase自动更新的价值。 Snapshot.key只是返回用户。这是我的JSON结构:

  {
users:{
-KQaU9lVcUYzIo52LgmN:{
device:e456f740-023e-440a
name:Test
}
},

如何得到 -KQaU9lVcUYzIo52LgmN 键?我想更新设备子。 。

  self.rootRef.child(users)。queryOrdered( ():childName());}} queryEqual(toValue:self.currentUser).observeSingleEvent(of:.value,with:{(snapshot)in 
let key = self.rootRef.child(users)。childByAutoId ).key
let childValues = [device:device]
self.rootRef.child(users)。child(key).updateChildValues(childValues)

解决方案当你获得Snapshot.key时,它会返回users,因为这是你的快照的整体关键。您的快照中的用户内的所有内容都被视为值。



您需要迭代子图层以挖掘到设备。 b
$ b

试试这个:

  rootRef.child(users)。observeSingleEventOfType(.Value ,withBlock:{(snapshot)in 
if result = snapshot.children.allObjects as?[FIRDataSnapshot] {
for child {
var userKey = child.key as!String
if(userKey == userKeyYouWantToUpdateDeviceFor){
rootRef.child(users)。child(userKey).child(device).setValue(device)
}
}






这段代码将执行以下操作:


  1. 获取引用的快照(关键是
    'users'

  2. 获取所有孩子(您的用户密钥),并将其作为另一个
    快照分配给'result'

  3. Chec ks每键一个,直到它找到你想要的键(例如,
    例如,如果您寻找用户的关键 - KQaU9lVcUYzIo52LgmN
    它会在您的示例代码中的第一次迭代中找到您
    发布的内容)。
  4. 一旦找到该键,它就会在该$ b中设置该设备的值
    rootRef.child(users)。child(userKey).child(device)。setValue(device)

当然,您需要在制作用户密钥时存储所有的用户密钥。你也可以在设备上使用SharedPreferences来做这件事,但是如果因为某种原因被清除,那么这些数据就会坐在那里。您也可以将它存储在您的应用程序的内部存储中,但SharedPreferences是我将使用的。



希望这有助于!


When I create objects in Firebase, I use childByAutoId. How can I update these specific objects later? I'm having trouble obtaining the value of the key Firebase automatically updates. Snapshot.key just returns "users". Here's my JSON structure:

{
  "users" : {
    "-KQaU9lVcUYzIo52LgmN" : {
      "device" : "e456f740-023e-440a"
      "name: "Test"
    }
  },

How can I get the -KQaU9lVcUYzIo52LgmN key? I want to update the device child. Here's what I have so far. It currently creates a completely separate snapshot with a single child.

self.rootRef.child("users").queryOrdered(byChild: "name").queryEqual(toValue: self.currentUser).observeSingleEvent(of: .value, with: { (snapshot) in
    let key = self.rootRef.child("users").childByAutoId().key
    let childValues = ["device": device]
    self.rootRef.child("users").child(key).updateChildValues(childValues)

Edit: device is a string set further up in the code. Not defined in this scope (to make it easier to read for this question).

解决方案

When you get Snapshot.key, it returns "users" because that is the overall key for your snapshot. Everything inside of "users" in your snapshot is considered the value.

You need to iterate over the child layers to dig down to "device".

Try this:

rootRef.child("users").observeSingleEventOfType(.Value, withBlock: { (snapshot) in 
    if let result = snapshot.children.allObjects as? [FIRDataSnapshot] {
        for child in result {
            var userKey = child.key as! String
            if(userKey == userKeyYouWantToUpdateDeviceFor){
                rootRef.child("users").child(userKey).child("device").setValue(device)
            }
        }
    }
})

This code will do the following:

  1. Gets snapshot of your reference (the key for that would be 'users').
  2. Gets all the children (your user keys) and assigns them as another snapshot to 'result'.
  3. Checks each key one at a time until it finds the key you want (for example, if you look for user with the key "-KQaU9lVcUYzIo52LgmN", it will find it on the first iteration in your example code you posted).
  4. Once it finds that key, it sets the value for the device inside that key with the line rootRef.child("users").child(userKey).child("device").setValue(device).

Of course, you will need to store all your user keys when you make them. You can maybe use SharedPreferences on the device for this, but if it gets cleared for any reason then that data will just be sitting there. You could also store it on internal storage for your app, but SharedPreferences is what I would use.

Hope this helps!

这篇关于如何使用childByAutoId更新Firebase中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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