本文介绍了Firebase实例和DatabaseReference的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Firebase作为我的应用程序的后端。 首先,我使用Firebase实例将对象保存到firbase数据库中,但是我必须更改实现以从保存的对象中获取密钥以供我的进一步实现。但是在更改实现之后,它会在保存时丢弃一些属性。 以下是两个实现的代码和屏幕截图。 Firebase ref = null; @Override protected void onCreate(Bundled 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); 用户publishere = new User(); Log.d( UUID,mAuth.getCurrentUser()。getUid()); publishere.setUUID(mAuth.getCurrentUser()。getUid()); ref.child(Advertisements)。 push()。setValue(mobileAdd); } 实现2. pre $ Database $ Reference $ ref $ null b $ b @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate (savedInstanceState); setContentView(R.layout.activity_ne w_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); 用户publishere = new User(); mobileAdd.setPublisher(publishere); $ b $ (); dbRef.setValue(mobileAdd); } } }); 以下是执行结果的截图。 在第二个实现中缺少突出显示的属性。为什么当我用 DatabaseReference 保存时,我缺少这些属性和 Firebase 引用我可以保存obejects而没有任何问题? 有什么特别的情况我们应该使用这两个方法? 为什么要选择一个方法。 如何在第二个实现中解决这个问题? 更新。 public class MobileAdd extends Add { private offer offers; 私人手机; 私人用户发布者; 私人清单< String> imagepaths; $ b公共MobileAdd(){} 公共MobileAdd(字符串描述,双重价格,日期日期){ super(描述,价格,日期); $ b公共MobileAdd(字符串描述,双重价格,日期日期,优惠活动,移动手机,用户发布者){ super(description,price,date ); this.offers =优惠; this.mobile =手机; this.publisher = publisher; } public offer getOffers(){ return offers; } public void setOffers(Offer offer){ 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; $ b 类添加 public class Add { private String key; 私有字符串描述; 私人双重价格; 私人日期日期; $ b 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; $ b $ p public class Mobile { private String manufacturer; 私有字符串模型; $ b public Mobile(){} public Mobile(String manufacturer,String model){ this.manufacturer = manufacturer; this.model = model; 保护移动(包裹){ 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; $ b 解决方案正如我在我的评论中提到的, Implementation1 使用了Firebase Legacy SDK,它使用Jackson来序列化/反序列化POJO。 Implementation2 使用了新的SDK,它不使用Jackson。支持的注释和接受的POJO表单在两个SDK之间是不同的。 $ b description 字段不是由 Implementation2 由于大写错误。 getter / setter方法应该是大写字母'D'的 getDescription()和 setDescription()。 另外, Date 不是一个简单的POJO,不会被新的SDK序列化。一种选择是使用 Date.getTime()并存储日期为一长。 I'm using firebase as back end for my application. First I was using Firebase instance to save object to firbase database which worked perfectly fine but I had to change the implementation to get key from object saved for my further implementation. but after changing implementation it discard some of properties when saving.Following are the codes and screenshots of both implementations.Implementation 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); }Implementation 2. DatabaseReference ref = null;@Overrideprotected 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); } } });Following is the screenshot with the implementation's result. Highlighted properties are missing in second implementation. Why when I save with DatabaseReference I'm missing these properties and with Firebase reference I can save obejects without any issues?Is there a special scenarios we should use these two methods?Why should we choose one method over another. How can I overcome this issue in the second implementation?Update.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; }}Class Add.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; }} 解决方案 As I mentioned in my comment, Implementation1 uses the Firebase Legacy SDK, which uses Jackson to serialize/deserialize POJOs. Implementation2 uses the new SDK, which does not use Jackson. The supported annotations and accepted POJO forms are different between the two SDKs.The description field is not written by Implementation2 because of a capitalization error. The getter/setter methods should be getDescription() and setDescription() with capital 'D'.Also, Date is not a simple POJO and will not be serialized by the new SDK. One option is to use Date.getTime() and store the date as a long. 这篇关于Firebase实例和DatabaseReference的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 02:09