将推送的ID保存在Firebase模型中 [英] Save pushed id in Firebase Model

查看:77
本文介绍了将推送的ID保存在Firebase模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有2个关于火力的问题:

I've got 2 firebase questions here :

A//解析值时,以编程方式将ID复制到本地模型(从数据库中排除)是一种好习惯吗?

A/ Is it a good practice to programmatically copy the ID to the local model (excluded from the database), when parsing a value ?

  • Frank, from Firebase, suggests that solution: Save userId on model Firebase

B/另一个解决方案是:

B/ Another solution would be to :

  • 生成新密钥
  • 使用该密钥并将其复制到模型中
  • 将模型(和密钥)保存到数据库

val newPostRef = ref.child("posts").push()
val key = newPostRef.key
newPostRef.setValue(Post(key, "title", "content"))

类似,帖子ID将由数据库模型保存,并使用 getValue(Post :: class)自动解析.

Like this, the post id will be hold by the database model and parsed automatically with getValue(Post::class).

我觉得这不是一个好的解决方案,但请告诉我原因:)

I can feel that is not a good solution, but please tell me why :)

致谢

推荐答案

两种解决方案都可以保证数据的一致性,但是就数据库大小而言,解决方案A优于解决方案B.

Both solutions guarantee data consistency, but solution A is better than solution B when it comes to database size.

使用解决方案B,您的数据库结构将如下所示:

By using solution B, your database structure would look like this:

"posts":{
    "nTY5U5NJJbPTJaPksPRNqau15H53" : {
      "message" : "Hello World",
      "sender" : "Rosário Pereira Fernandes",
      "key" : "nTY5U5NJJbPTJaPksPRNqau15H53"
    }
}

这应该使用大约 180个字节的磁盘空间.请注意,此密钥在您的节点上重复两次.为什么不删除它以节省空间?

This should use around 180 bytes of disk space. Notice that this key is repeated twice on your node. Why not remove it to save space?

使用解决方案B,您将拥有一个较小的数据库:

Using solution B, you'd have a smaller database:

"posts":{
    "nTY5U5NJJbPTJaPksPRNqau15H53" : {
      "message" : "Hello World",
      "sender" : "Rosário Pereira Fernandes"
    }
}

这将使用大约 135个字节.这比解决方案B小了 45个字节.现在,假设您的数据库中是否有1000个帖子.您将在解决方案B上多使用 45000字节.这足够存储大约300个帖子,但被多余的key属性占用.

This would use around 135 bytes. That's 45 bytes less than solution B. Now imagine if you had 1000 posts on your database. You'd be using 45000 bytes more on solution B. This is enough space to store around 300 more posts, but it is being taken by the extra key attribute.

请不要忘记, Firebase数据库对于存储的GB和下载的GB有一定的价格限制 .通过使用解决方案B,您将比使用解决方案A更快地达到此限制.

Don't forget that the Firebase Database has some price limitations for GB stored and GB downloaded. By using solution B you would reach this limit faster than by using solution A.

这篇关于将推送的ID保存在Firebase模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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