在单个值上使用childByAutoId? [英] Using childByAutoId On Single Value?

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

问题描述

我对Swift和Firebase都很陌生,我试图做一个简单的应用程序使用Firebase作为后端。据我所知,没有内存有效的方式来使用numChildren()函数而不加载每个孩子到内存中进行计数,所以我正在实现我自己的简单计数器的事件我的应用程序

Firebase的文档声明应该使用childByAutoID()方法来更新多用户应用程序中的列表。我假设它为请求的更新添加时间戳并按顺序执行它们。

我的问题是在多用户应用程序中只更新SINGLE字段时是否需要使用childByAutoID()。也就是说,如果我这样做,我的numEvents字段会有冲突:
$ b $ pre $ dbRef = FIRDatabase.database()。reference()
dbRef.child(numEvents)。setValue(num)



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ db $ $ $ $ $ $ $ $ ().setValue(num)

为了避免写冲突?我唯一真正的困惑是childByAutoID的文档强调,当孩子是一个项目列表,但我的只是一个单一的项目是有用的。

解决方案childByAutoId做的是为一个节点创建一个唯一的键,以避免多次使用相同的键,然后创建不一致的数据冲突(不写冲突),以避免使用事务块的写冲突。

/ p>

最好的学习方法是试试看

如果num == 1,在第一个例子中结果将是

  dbRef:{
numEvents:1
}



第二个将是

  dbRef:{
numEvents:{
//自动生成的键
KLBHJBjhbjJBJHB:1
}
}

如果您想要保存同一类型的多个子节点,那么childByAutoId会很有用,这样每个孩子都会拥有自己的唯一标识符



例如

  pet:{
KJHBJJHB:{
名字:蓬松,
所有者:约翰·史密斯,
},
KhBHJBJjJ:{
名字:蓬松,
所有者:简Foster,
}
}

通过这种方式,那里没有明确的方式与项目数据,以保证它是唯一的(在这种情况下,宠物的名字)


I am pretty new to both Swift and Firebase, and I am attempting to make a simple app using Firebase as the backend. As far as I know, there is no memory-efficient way to use the numChildren() function without loading every single child into memory for counting, so I am implementing my own simple counter for the number of "Events" that have been created in my app.

The documentation for Firebase states that the childByAutoID() method should be used for updating lists in multi-user applications. I am assuming it adds a timestamp to the requested update and does them in order.

My question is whether it is necessary to use childByAutoID() when only updating a SINGLE field in a multi-user application. That is, will there be conflicts on my numEvents field if I do:

dbRef = FIRDatabase.database().reference()
dbRef.child("numEvents").setValue(num)

Or must I do:

dbRef = FIRDatabase.database().reference()
dbRef.child("numEvents").childByAutoId().setValue(num)

In order to avoid write conflicts? My only real confusion is that the documentation for childByAutoID stresses that it is useful when the children are a list of items, but mine is only a single item.

解决方案

What childByAutoId does is create a unique key for a node, to avoid using the same key multiple times and then creating data conflicts like inconsistency (not write conflicts) to avoid write conflicts you use the transaction blocks.

The best way to learn is to try it out

If num == 1 , in the first example the result will be

dbRef:{
  numEvents:1
}

While the second will be

dbRef:{
  numEvents:{
    //The auto-generated key
    KLBHJBjhbjJBJHB:1
  }  
}

The childByAutoId would be useful if you want to save in a node multiple children of the same type, that way each children will have its own unique identifier

For example

pet:{
  KJHBJJHB:{
    name:fluffy,
    owner:John Smith,
  },
  KhBHJBJjJ:{
    name:fluffy,
    owner:Jane Foster,
  }
} 

This way you have a unique identifier for cases where there is no clear way with the item data to guarantee it will be unique (in this case the pet's name)

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

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