2

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:"[email protected]"
      },
    ...

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:"[email protected]",
      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!

1
  • I've read Firebase Structure Data but it doesn't specific how to create those structures Commented Mar 19, 2017 at 23:01

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

This assumes all events in the schedule have different names.
I think the Map object was what I'm missing, thanks!
@JamesB: for Firebase Database this is the recommended structured for list-like structures. See firebase.google.com/docs/database/web/… and firebase.googleblog.com/2014/04/…
@FrankvanPuffelen My comment is more Java-specific than any library/database. If the key for the map is the name of the event, two different events with the same name will overwrite each other when stored in Schedule. If the key is unique per event then my initial comment can be ignored.
The Schedule map in the above would be keyed on the node name in the JSON structure. So event1 and event2 in the above sample, but in most Firebase Database projects they would be [auto-generated push IDs](firebase.googleblog.com/2015/02/the-2120-ways-to-ensure-unique_68.html).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.