Firebase插入与生成的密钥

Firebase插入与生成的密钥

本文介绍了Firebase插入与生成的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在firebase中有一个根,就像我试图让用户删除列表中的项目一样.但是用户可以放弃自己的决定.当用户放弃此决定时,我想在数据库中再次插入已删除的项目.但是,我要插入旧的Firebase生成密钥,因为我正在使用Firebase推键.这是一个不好的做法. Firebase如何生成这些密钥?它是否检查db上的每个密钥并生成一个新密钥?有没有可能将该密钥标记为已删除并在以后为另一个项目生成?很抱歉这门语言.很难表达.

I have a root in firebase like in the I am trying to enable user to delete an item on list. But user can give up his decision. When user give up this decision, I want to insert the deleted item again in the database. But, I want to insert with old firebase generated key, because I am using firebase push keys. Is that a bad practice. How firebase generate these keys? Does it checks every key on db and generate a new one? Is that any possibility, that key marked as removed and generated later for another item? Sorry for the language. It has been hard to express.

我想使用旧密钥,因为我正在使用orderByKey获取数据.我不想失去订单.

EDITED: I want to use the old key because, I am getting the data with orderByKey. I dont want to lose order.

推荐答案

每当您在数据库引用上使用push时,都会使用包含服务器时间戳记的唯一键生成一个新的数据节点.这些键看起来像-KiGh_31GA20KabpZBfa.

Whenever you use push on a Database Reference, a new data node is generated with a unique key that includes the server timestamp. These keys look like -KiGh_31GA20KabpZBfa.

由于有时间戳记,因此可以确保给定的键是唯一的,而不必检查数据库中的其他键.

Because of the timestamp, you can be sure that the given key will be unique, without having to check the other keys inside your database.

否,无论是否移除了一个键,两个键都不可能发生碰撞.

No, it is not possible that two keys will collide, regardless of wether one has been removed or not.

不幸的是,仅使用push不能两次生成相同的密钥.因此,不可能删除具有给定密钥的节点,然后再使用push将其再次插入具有相同密钥的同一路径,因为push会生成不同且唯一的密钥.

Unfortunately, you can't generate the same key twice by just using push. So, it is not possible to delete a node with a given key and then use push to insert it again at the same path with the same key, because push would generate a different and unique key.

相反,如果按键排序对您来说很重要,并且有可能重新插入已删除的节点,那么我建议您执行以下操作之一:-

Instead of this, if ordering by key is that important to you, and there's a possibility that a deleted node can be reinserted then I would recommend you to do one of the following :-

  1. 从数据库中删除密钥后,要么将其保存在客户端,要么在需要重新插入时使用.

  1. Either save the key on the client side when it's deleted from the database, and use it when you need to reinsert.

或者,也许在您的数据库中有一个"deleted-keys"路径,然后将已删除的键保存在那里.当然,使用这种方法,您需要存储其他信息以标识密钥对应的数据.

Or , maybe, have a "deleted-keys" path in your database and save the deleted keys there. Of course, with this approach, you'd need to store additional information to identify the data that the key corresponds to.

这实际上取决于您的用例.

It all really depends on your use case.

这篇关于Firebase插入与生成的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:51