firebase数据库:与Kotlin委托属性的模型 [英] firebase db: models with Kotlin delegated properties
问题描述
我使用Kotlin对象来处理我的Firebase数据库模型,如在指南。我有很多字段被存储为字符串,但实际上是枚举,所以是类型安全的我在模型中有枚举字段,再加上一个字符串委托属性,返回firebase存储值(如
抽象类BaseModel {
@Exclude
open var path:String? = null //失败,即使我删除这个领域!
$ B $构造函数BaseModel()$ $ $ $构造函数v:Double,u:WeightUnit:this(){
value = v
unitEnum = u
var value:Double = 0.0
@Exclude
var unitEnum:WeightUnit = WeightUnit.KG
var unit:String通过EnumStringLowercaseConverter(WeightUnit :: class.java).getDelegate(Weight :: unitEnum)
$ b [...]
val testWeight = Weight(7.0 ,kg)
db.getReference(/ valid / path / to / save / testWeight)。setValue(testWeight)
.addOnSuccessListener {r - > Log.d(LOG_TAG,设置成功)}
.addOnFailureListener {e - > Log.e(LOG_TAG,set error,e)}
setValue
始终给出权限被拒绝
错误,但是,如果我删除 unitEnum
make unit
正常 String
属性。
阅读:Firebase在获取Weight对象时不会出现错误,但 weightUnit
字段永远不会被设置为默认值。但是,如果我手动执行 weight.unit =lb
, unitEnum
字段正确返回 WeightUnit.LB
。
我使用的是firebase版本库v10.0.1
现在,问题:
- 我可以做些什么来使委派属性与firebase正常工作?我可以尝试不同的方法来委托枚举字段,只要从
- 有什么办法可以看到 做firebase库将对象转换为/从json?或者至少看到转换后的json?也许我可以自己调整一下。不幸的是,在AndroidStudio中,与firebase相关的所有内容都显示为
/ *编译代码* /
。
UPDATE :我当然可以为每个模型添加一个toMap()
方法,在这里我将构造一个包含所有需要的属性在firebase中,但是对于每一个模型来说这样做会很麻烦,而且它只解决了保存问题,枚举字段在获取的时候仍然不会被设置。
使用GSON序列化时,也会跳过委托的道具。所以也许有一个通用的方法,使委派属性看起来像普通的领域?试试这个代码,它应该工作。
@get:排除@set:排除
var unitEnum:WeightUnit = WeightUnit.KG
var unit :String
get()= unitEnum.name $ b $ set(v){unitEnum = WeightUnit.valueOf(v)}
I'm using Kotlin objects to work with my Firebase Database models, as described in the guide. I have many fields that are stored as strings, but really are enums, so to be type-safe I have enum fields in the models, plus a string delegated property that returns the firebase stored value (as suggested in a question I asked some time ago). Now, these fields work if I get/set the string delegate in code, but firebase libs seem to skip them when converting to/from database's json format.
A simple example:
abstract class BaseModel { @Exclude open var path: String? = null // fails even if I delete this field! } class Weight() : BaseModel() { constructor(v: Double, u: WeightUnit) : this() { value = v unitEnum = u } var value: Double = 0.0 @Exclude var unitEnum: WeightUnit = WeightUnit.KG var unit: String by EnumStringLowercaseConverter(WeightUnit::class.java).getDelegate(Weight::unitEnum) } [...] val testWeight = Weight(7.0, "kg") db.getReference("/valid/path/to/save/testWeight").setValue(testWeight) .addOnSuccessListener { r -> Log.d(LOG_TAG, "set successful") } .addOnFailureListener { e -> Log.e(LOG_TAG, "set error", e) }
The
setValue
always gives aPermission Denied
error, but works, if I deleteunitEnum
field and makeunit
a normalString
property.It's similar for reading: Firebase gives no errors when getting a Weight object, but the
weightUnit
field is never set to anything else than the default. But, if I manually doweight.unit = "lb"
, theunitEnum
field properly returnsWeightUnit.LB
.I'm using firebase libs v10.0.1
Now, the questions:
- What can I do to make the delegated properties work correctly with firebase? I can try a different approach to the delegated enum fields, as long as the points from my original question are satisfied (readable, concise and type-safe code).
- is there any way to see how exactly do firebase libs convert objects to/from json? Or at least see the converted json? Maybe then I could tweak things myself. Unfortunately, everything firebase-related shows as
/* compiled code */
in AndroidStudio.
UPDATE: I could of course add a
toMap()
method to each model, where I would construct a map containing all the properties needed in firebase, but it would be tiresome to do this for every model, and it solves the saving issue only, the enum fields still wouldn't be set when getting.The delegated props are also skipped when serializing with GSON. So maybe is there a generic way to make the delegated properties look like regular fields?
解决方案Try this code, it should work.
@get:Exclude @set:Exclude var unitEnum: WeightUnit = WeightUnit.KG var unit: String get() = unitEnum.name set(v) { unitEnum = WeightUnit.valueOf(v) }
这篇关于firebase数据库:与Kotlin委托属性的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 有什么办法可以看到 做firebase库将对象转换为/从json?或者至少看到转换后的json?也许我可以自己调整一下。不幸的是,在AndroidStudio中,与firebase相关的所有内容都显示为