Firebase和自定义对象中的嵌套地图 [英] Nested maps in Firebase and custom objects

查看:259
本文介绍了Firebase和自定义对象中的嵌套地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对这个基本问题表示歉意,但是我的代码遇到了一些麻烦.

Apologies for the rather basic question but I am having a little trouble with my code.

我打算创建一个事件数据库,并且希望灵活地检索一整天的事件,或者一个月甚至几年的事件.因此,我决定将数据存储在年">月">天"下.最初,我尝试使用我认为更易于使用的自定义Java对象来完成此操作,但是不幸的是,这在每个对象之间使用getter方法的名称添加了一个额外的子节点. 我使用长嵌套的哈希映射实现了我想要的: Map<String,Map<String,Map<String,Map<String,Object>>>> mMap = new HashMap<>();

I intended to create a database of events and I wanted the flexibility of retrieving an entire day's worth of events, or a month's worth or even a years worth. So I decided to store the data under 'year'>'month'>'day'. Initially I tried to do it using custom java objects that I thought would be easier to work with, but unfortunately this adds an extra child node between each object with the name of the getter method. I achieved what I wanted using long nested hash maps: Map<String,Map<String,Map<String,Map<String,Object>>>> mMap = new HashMap<>();

但我确信必须有更好的方法.

but I am sure that there must be a better way.

我保存了一张图片,但是我的声誉< 10无法将其发布. 链接到json地图,显示我的自定义对象所产生的效果与嵌套地图所实现的预期结果

I have saved a picture but I can't post it as my reputation <10. link to json map showing what my custom objects made vs the intended result achieve by the nested maps

这是第一个地图的类(不包括对象类,因为它有点长):

so here is the class for the first map (object class not included as it is a bit long):

public class DayKeyBookingsMap {
private Map<String, BookingObject> dayKeyBookingsMap = new HashMap<>();

public DayKeyBookingsMap(){}
public DayKeyBookingsMap(BookingObject bookingObject){
    dayKeyBookingsMap.put(bookingObject.getDateKey(),bookingObject);
}

public Map<String, BookingObject> getDayKeyBookingsMap() {
    return dayKeyBookingsMap;    }

public void setDayKeyBookingsMap(Map<String, BookingObject> dayKeyBookingsMap) {
    this.dayKeyBookingsMap = dayKeyBookingsMap;    }
}

以及下一级别的单独课程:

and a separate class of the next level:

public class MonthKeyBookingsMap {
private Map<String,DayKeyBookingsMap> monthKeyBookingsMap = new HashMap<>();

public MonthKeyBookingsMap(){}
public MonthKeyBookingsMap(BookingObject bookingObject){
    DayKeyBookingsMap dayKeyBookingsMap = new DayKeyBookingsMap(bookingObject);
    monthKeyBookingsMap.put(bookingObject.getMonthKey(),dayKeyBookingsMap);
}

public Map<String, DayKeyBookingsMap> getMonthKeyBookingsMap() {
    return monthKeyBookingsMap;
}

public void setMonthKeyBookingsMap(Map<String, DayKeyBookingsMap> monthKeyBookingsMap) {
    this.monthKeyBookingsMap = monthKeyBookingsMap;
}
}

,我还没有包括第三级.但是我用以下方法构造了最终对象:

and I haven't included the third level. But I constructed the final object with:

YearKeyBookingsMap(myBasicObject)

称为第三级构造,依次使用了第二级构造等.

which called the third level constructure, which in turn used the second level constructor etc.:

public YearKeyBookingsMap(BookingObject bookingObject){
    MonthKeyBookingsMap monthKeyBookingsMap = new MonthKeyBookingsMap(bookingObject);
    yearKeyBookingsMap.put(bookingObject.getYearKey(),monthKeyBookingsMap);
}

推荐答案

这对Firebase的工作原理无济于事或无济于事...

This does not help or teach anything about how firebase works...

Map<String, Object> map = new HashMap<String, Object>();
    map.put("dateKey", "20170105");
    map.put("eventTitle", "New Booking");
    map.put("monthKey", "01");
    map.put("yearKey", "2017");
    FirebaseDatabase.getInstance().getReference().child("AAAAB").child("2017").child("01").child("20170105").setValue(map);

您可以修复数据结构,让年份作为孩子/钥匙并没有帮助.如果您打算将其用于将来的索引或分组,则通过访问dateKey中的值将获得相同的结果.从这里开始,您要做的就是:

You could fix your data structure, having the year be a child/key isn't as helpful. If you were doing it for future indexing or grouping you would get same result by accessing the value in dateKey. From here all you would have to do is:

mRef = FirebaseDatabase.getInstance().getReference().child(userID);
mRef.addValueEventListnere (blah blah){
 for (DataSnapShot snapShot : dataSnapShot.getChildren())
UniqueData currData = snapShot.getValue(UniqueData.class)
   currData.getDateKey();
}

UniqueData类仅是构造函数和设置器/获取器

Where UniqueData class is nothing more than a constructor and setters/getters

这篇关于Firebase和自定义对象中的嵌套地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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