Firebase实例与DatabaseReference之间的区别

时间:2017-01-08 17:42:41

标签: android firebase firebase-realtime-database

我正在使用firebase作为我的应用程序的后端。 首先,我使用Firebase实例将对象保存到firbase数据库,该数据库工作得非常好但是我必须更改实现以从保存的对象获取密钥以供我进一步实现。但在更改实施后,它会在保存时丢弃一些属性。

以下是两种实现的代码和屏幕截图。

实施1。

 Firebase ref = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_advertisement);
        ref = new Firebase("https://xxxxxxxxxxxx.firebaseio.com/");
    }
    addAdvertisement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


    Mobile mobile = new Mobile(lclManu, lclMdl);
        MobileAdd mobileAdd = new MobileAdd();

        mobileAdd.setMobile(mobile);
        mobileAdd.setPrice(lclPrice);
        mobileAdd.setdescription(lclDes);
        mobileAdd.setDate(date);


        User publishere = new User();
        Log.d("UUID", mAuth.getCurrentUser().getUid());
        publishere.setUUID(mAuth.getCurrentUser().getUid());

        ref.child("Advertisements").push().setValue(mobileAdd);
        }

实施2。

    DatabaseReference ref = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_advertisement);
    ref = FirebaseDatabase.getInstance().getReference();

    addAdvertisement.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Mobile mobile = new Mobile(lclManu, lclMdl);
        MobileAdd mobileAdd = new MobileAdd();

        mobileAdd.setMobile(mobile);
        mobileAdd.setPrice(lclPrice);
        mobileAdd.setdescription(lclDes);
        mobileAdd.setDate(date);

        User publishere = new User();
        mobileAdd.setPublisher(publishere);

        DatabaseReference dbRef = ref.child("Advertisements").push();
        dbRef.setValue(mobileAdd);
    }
    }
    });

以下是实施结果的屏幕截图。 Result image

第二次实施中缺少突出显示的属性。为什么当我使用DatabaseReference保存时,我缺少这些属性并使用Firebase引用我可以保存对象而不会出现任何问题?

  1. 是否有特殊情况我们应该使用这两种方法?
  2. 我们为什么要选择一种方法而不是另一种方法。
  3. 如何在第二次实施中克服此问题?
  4. 更新

    public class MobileAdd extends Add {
    
    
        private Offer offers;
        private Mobile mobile;
        private User publisher;
        private List<String> imagepaths;
    
        public MobileAdd() {
        }
    
        public MobileAdd(String description, double price, Date date) {
            super(description, price, date);
        }
    
    
        public MobileAdd(String description, double price, Date date,Offer offers, Mobile mobile, User publisher) {
            super(description, price, date);
            this.offers = offers;
            this.mobile = mobile;
            this.publisher = publisher;
        }
    
        public Offer getOffers() {
            return offers;
        }
    
        public void setOffers(Offer offers) {
            this.offers = offers;
        }
    
        public Mobile getMobile() {
            return mobile;
        }
    
        public void setMobile(Mobile mobile) {
            this.mobile = mobile;
        }
    
        public User getPublisher() {
            return publisher;
        }
    
        public void setPublisher(User publisher) {
            this.publisher = publisher;
        }
    
        public List<String> getImagepaths() {
            return imagepaths;
        }
    
        public void setImagepaths(List<String> imagepaths) {
            this.imagepaths = imagepaths;
        }
    }
    

    课程添加。

    public class Add  {
    
        private String key;
        private String description;
        private double price;
        private Date date;
    
        protected Add() {
        }
        public Add(String description, double price, Date date) {
            this.description = description;
            this.price = price;
            this.date = date;
        }
    
        public String getdescription() {
            return description;
        }
    
        public void setdescription(String description) {
            this.description = description;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        public String getKey() {
            return key;
        }
    
        public void setKey(String key) {
            this.key = key;
        }
    }
    

    Class Mobile

     public class Mobile{
    
        private String manufacturer;
        private String model;
    
        public Mobile() {
        }
    
        public Mobile(String manufacturer, String model) {
            this.manufacturer = manufacturer;
            this.model = model;
        }
    
        protected Mobile(Parcel in) {
            manufacturer = in.readString();
            model = in.readString();
        }
        public String getManufacturer() {
            return manufacturer;
        }
    
        public void setManufacturer(String manufacturer) {
            this.manufacturer = manufacturer;
        }
    
        public String getModel() {
            return model;
        }
    
        public void setModel(String model) {
            this.model = model;
        }
    
    }
    

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的, Implementation1 使用Firebase Legacy SDK,它使用Jackson来序列化/反序列化POJO。 Implementation2 使用新的SDK,它不使用Jackson。支持的注释和接受的POJO表单在两个SDK之间是不同的。

由于大小写错误, Implementation2 不会写入description字段。 getter / setter方法应为getDescription()setDescription(),其中包含大写&#39; D&#39;

此外,Date不是简单的POJO,也不会被新SDK序列化。一种选择是使用Date.getTime()并将日期存储为长。