创建嵌套对象Firebase结构 [英] Creating Nested Object Firebase structure

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

问题描述

我使用Firebase作为我的Android项目的数据库。在这个项目中,我的用户数据最少。

{
显示:姓名,
电子邮件:foo@bar.ca
},
...
pre>

我用一个带有两个本地字符串变量的基本对象User创建了这个结构。



我的问题是我现在需要为每个用户添加一个时间表,每个用户都有一个事件列表,每个事件都有自己的信息(开始时间,结束时间等)。

我知道我的结构可能看起来像这样:

  users {
1234-567-897:{
显示:姓名,
电子邮箱:foo@bar.ca,
计划:{
event1:{
name:Meeting,
startTime:1,
endTime:2
},
event2:{...}
},
Sschedule2:{...}
},



<但是我不知道如何去创建这种类型的Firebase结构。我最初将使用一个ArrayList来保存Schedules和Events的对象,但在阅读Firebase文档之后,看起来他们不支持ArrayList存储。我正在考虑的一个替代方案是创建一个单独的日程表并引用它们。

解决方案

这些似乎是您的数据所需的最小类:

  public class Event {
public String name;
public long startTime;
public Long endTime;
}

public class User {
public String Display;
公共字符串电子邮件;
public Map< String,Event>时间表;

$ / code>

Schedule 是一个地图,其中的键是 event1 event2



我没有添加 Schedule2 ,因为它的类型是未指定的。


I'm using Firebase as my database for my Android project. In this project, I have users with minimal data associated with them.

    users{
      1234-567-897:{
          Display:"Name",
          Email:"foo@bar.ca"
      },
    ...

I made this structure with a basic object User with two local string variables.

My issue is that I now need to add a schedule to each user, which consists of a list of Events, each event with it's own information(Start time, End time, etc.)

I understand that my structure would probably look something like this:

users{
  1234-567-897:{
      Display:"Name",
      Email:"foo@bar.ca",
      Schedule:{
          event1:{
               name:"Meeting",
               startTime:1,
               endTime:2
          },
          event2:{...}
      },
      Sschedule2:{...}
  },

But I have no idea how to go about creating this type of structure with Firebase. I was initially going to use an ArrayList to hold the objects of Schedules and Events but after reading the Firebase documentation, it looks like they don't support ArrayList storing. An alternative to this that I was considering was to create a separate table of schedules and reference them with uids.

Appreciate any advise regarding creating this structure, thanks!

解决方案

These seem to be the minimal classes needed for your data:

public class Event {
  public String name;
  public Long startTime;
  public Long endTime;
}

public class User { 
  public String Display;
  public String Email;
  public Map<String, Event> Schedule;
}

The Schedule is a map, where the keys are event1 and event2.

I didn't add Schedule2 because its type is unspecified.

这篇关于创建嵌套对象Firebase结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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