如何管理Firebase数据库密钥? [英] How to manage firebase database keys?

查看:159
本文介绍了如何管理Firebase数据库密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用firebase(或任何基于id的数据集合数据)时,我几乎总是要跟踪给定值的关键字。例如,让我们假设我有位置类与纬度和经度字段。当我从firebase下载的时候,除了它的两个字段之外,我还想跟踪关键字(使用push()生成的节点值,例如-K_esEYXNfMBNmgF3fO4),它被下载,所以我稍后可能会更新它,删除它等。只有两个解决方案:


  1. 复制数据并将键值添加为另一个Location类字段。这不能很好地工作,因为我必须在执行push()之后设置键值。

  2. 创建通用的包装类,保持键和对象:

      public class Key< T> {

    private final String key;
    私人最终T值;

    公钥(字符串键,T值){
    this.value = value;
    this.key = key;
    }

    public String key(){
    return key;
    }

    public T value(){
    返回值;


    code


    我正在使用第二种方法,但看起来不太好。我有这个Key类基本上遍及我所有的代码库,当使用RxJava时,大量的方法有这样的返回类型: Observable< Key< Location>>

    解决方案

    实际上,我觉得这很荒唐。

    或者,您可以在POJO中包含密钥并使用 @Exclude 进行注释,以将其排除在序列化之外。

    When using firebase (or any database that aggregates data basing on ids) I nearly always have to keep track of a key of a given value. For example let's assume I have Location class with latitude and longitude fields. When I download if from firebase, besides its two fields, I also want to keep track of the key (node value generated with push() e.g. -K_esEYXNfMBNmgF3fO4) it was downloaded from so I may later update it, delete it etc. I see only two solutions:

    1. Duplicate the data and add key value as another Location class field. That doesn't work nicely because I have to set the key value only after I executed push().

    2. Create generic wrapper class that will keep key and object:

      public class Key<T> {
      
        private final String key;
        private final T value;
      
        public Key(String key, T value) {
            this.value = value;
            this.key = key;
        }
      
        public String key() {
            return key;
        }
      
        public T value() {
            return value;
        }
      }
      

    I am using the second approach but it doesn't look really nice. I have this Key class basically throughout all my codebase and when using RxJava plenty of methods have a return type like this: Observable<Key<Location>> and that just looks ridiculous.

    解决方案

    What you call ridiculous actually looks quite normal to me.

    Alternatively you can include the key in the POJO and annotate it with @Exclude to exclude it from the serialization.

    这篇关于如何管理Firebase数据库密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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