Firebase的setValue()如何用于Java中的对象? [英] How does Firebase's setValue() work for objects in Java?

查看:165
本文介绍了Firebase的setValue()如何用于Java中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firebase有关列表的文档中,有一个例子,您可以创建Java对象并通过 setValue()将该对象写入Firebase引用,如下所示:

  private static class MyObject {

private String property1;
private int property2;

public MyObject(String value1,int value2){
this.property1 = value1;
this.property2 = value2;
}

public String getFirstProperty(){
return property1;
}

}

private void populateList(){
Firebase ref = new Firebase(https://MyDemo.firebaseIO-demo.com / myObjects);
ref.push()。setValue(new MyObject(myString,7));



$ b $ p
$ b

这是如何在内部工作的,即当你没有写 toString()方法等等,什么值将完全保存到Firebase参考中?更进一步,Firebase客户端能够从保存的值恢复旧对象吗?如何?



是否有必要使用私有静态类,以便Firebase可以读取字段?

解决方案

文档解释了这是如何工作的:



lockquote

在这个位置的数据到给定的值。该方法接受的值的本地类型对应于JSON类型:


  • 布尔值
  • Long

  • Double
  • Map< String,Object>

  • List< ; Object>



另外,你可以将你自己的类的实例设置到这个位置,满足以下约束:


  1. 类必须有一个不带参数的默认构造函数
  2. 类必须为要分配的属性定义公共getter。当一个实例被反序列化时,没有公共getter的属性将被设置为它们的默认值。所以,您必须为您想要保存的所有属性创建获取者。在你的例子中,你的 firstProperty 属性将被写入,尽管由于你没有定义默认的构造函数而不能读取它。 b $ b

    In Firebase's documentation about lists, there is an example where you create a Java object and write that object to a Firebase reference via setValue() like this:

    private static class MyObject {
    
        private String property1;
        private int property2;
    
        public MyObject(String value1, int value2) {
            this.property1 = value1;
            this.property2 = value2;
        }
    
        public String getFirstProperty() {
            return property1;
        }
    
    }
    
    private void populateList() {
        Firebase ref = new Firebase("https://MyDemo.firebaseIO-demo.com/myObjects");
        ref.push().setValue(new MyObject("myString", "7"));
    }
    

    How does this work internally, i.e. when you have not written a toString() method and so on, what value will be saved to the Firebase reference exactly? And going one step further, will the Firebase client be able to restore the old object from the saved value? How?

    Is it necessary to have a private static class so that Firebase can read the fields?

    解决方案

    The documentation explains how this works:

    Set the data at this location to the given value. The native types accepted by this method for the value correspond to the JSON types:

    • Boolean
    • Long
    • Double
    • Map<String, Object>
    • List<Object>

    In addition, you can set instances of your own class into this location, provided they satisfy the following constraints:

    1. The class must have a default constructor that takes no arguments
    2. The class must define public getters for the properties to be assigned. Properties without a public getter will be set to their default value when an instance is deserialized

    So, you must create getters for all the properties that you want saved. In your example, your firstProperty property will be written, though it won't be possible to read it back because you haven't defined a default constructor.

    这篇关于Firebase的setValue()如何用于Java中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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