如何管理Firebase数据库密钥? [英] How to manage firebase database keys?
问题描述
-
复制数据并将键值添加为另一个Location类字段。这不能很好地工作,因为我必须在执行push()之后设置键值。
创建通用的包装类,保持键和对象: 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().
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; } }
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:
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屋!