0

I am migrating my app to use Firebase Firestore, and one of my models is very complex (contains lists of other custom objects). Looking at the documentation, on how to commit a model object as a document, it looks like you simply create your model object with a public constructor, and getters and setters.

For example from the add data guide:

public class City {
    private String name;
    private String state;
    private String country;
    private boolean capital;
    private long population;
    private List<String> regions;

    public City() {}

    public City(String name, String state, String country, boolean capital, long population, List<String> regions) {

    // getters/setters
}

Firestore automatically translates this to and from and document without any additional steps. You pass an instance to a DocumentReference.set(city) call, and retrieve it from a call to DocumentSnapshot.toObject(City.class)

How exactly does it serialize this to a document? Through reflection? It doesn't discuss any limitations. Basically, I'm left wondering if this will work on more complex models, and how complex. Will it work for a class with an ArrayList of custom objects?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
rosghub
  • 8,924
  • 4
  • 24
  • 37
  • you asked alot of things in a single question. – Nouman Ch Jan 15 '19 at 06:27
  • 1
    It uses reflection. This is common for systems that map data into POJO (JavaBean) type objects. Only getters and setters are used, or public member fields. – Doug Stevenson Jan 15 '19 at 06:32
  • @DougStevenson Thank you. Will it work for lists of custom objects that eventually boil down to POJO types? Like `A` contains a list of `B` which contains a list of `C` which contains POJO types – rosghub Jan 15 '19 at 06:34
  • I'm not sure what you mean by that. I suggest you write some code to do what you want, then post back here with a description of what you're concretely trying to do and the code that doesn't work the way you expect. – Doug Stevenson Jan 15 '19 at 06:46

1 Answers1

5

Firestore automatically translates this to and from and document without any additional steps. How exactly does it serialize this to a document? Through reflection?

You're guessing right, through reflection. As also @Doug Stevenson mentioned in his comment, that's very common for systems as Firebase, to convert JSON data to POJO (Plain Old Java Object). Please also note that the setters are not required. If there is no setter for a JSON property, the Firebase client will set the value directly onto the field. A constructor-with-arguments is also not required. While both are idiomatic, there are good cases to have classes without them. Please also take a look at some informations regarding the existens fo the no-argument constructor.

It doesn't discuss any limitations.

Yes it does. The official documentation explains that the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:

Maximum size for a document: 1 MiB (1,048,576 bytes)

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much but as your array getts bigger (with custom objects), be careful about this limitation.

Please also note, that if you are storing large amount of data in arrays and those arrays should be updated by lots of users, there is another limitation that you need to take care of. So you are limited to 1 write per second on every document. So if you have a situation in which a lot of users al all trying to write/update data to the same documents all at once, you might start to see some of this writes to fail. So, be careful about this limitation too.

Will it work for a class with an ArrayList of custom objects?

It will work with any types of classes as long as are supported data type objects.

Basically, I'm left wondering if this will work on more complex models, and how complex.

It will work with any king of complex model as long as you are using the correct data types for your objects and your documents are within that 1 MIB limitation.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193