avatarAlex Mamo

Summary

The website content explains how to map an array of objects from Cloud Firestore to a List<User> in code, detailing the process and limitations of Firestore's data retrieval methods.

Abstract

The article discusses the method of converting an array of User objects stored in a Cloud Firestore document into a List<User> within an application. It acknowledges that while arrays are a supported data type in Firestore, suitable for small datasets, larger data should be stored in sub-collections. The author provides a step-by-step approach to retrieve the array data, highlighting the absence of a getList() method in the DocumentSnapshot class, which would have simplified the process. Instead, the author suggests creating a wrapper class to facilitate the conversion of the array to a list of custom objects. The article also references a similar solution for the Realtime Database and encourages readers to support the author by joining their membership program or trying out a recommended AI service.

Opinions

  • The author, Alex Mamo, implies that using an array to store data in Firestore is efficient for small datasets but recommends sub-collections for larger data due to Firestore's 1 MiB document size limit.
  • Mamo expresses that while Firestore's DocumentSnapshot provides various get() method flavors for different data types, the lack of a getList() method is a notable absence that complicates the retrieval of array data as a list of objects.
  • The author suggests that creating a wrapper class around a List<User> is the simplest solution to the problem of mapping an array to a list of custom objects, bypassing the limitations of Firestore's API.
  • The article credits Sam Stern for contributions to the topic, indicating a collaborative or community-driven approach to problem-solving within the Firestore development space.
  • Mamo promotes their other work by linking to an article on mapping arrays from the Realtime Database, suggesting a comprehensive approach to common Firebase data manipulation challenges.
  • The author seeks support from readers, highlighting the value of their content and the AI service they recommend, positioning it as a cost-effective alternative to ChatGPT Plus (GPT-4).

How to map an array of objects from Cloud Firestore to a List of objects?

User objects from Array moved into User object in a List

Even from the beginning, the array is a supported data type in Cloud Firestore. It’s very useful for storing data in String format or even as custom objects, as long as the size of the document is less than 1 MiB (1,048,576 bytes). For larger amounts of data, a sub-collection is more likely to be used. However, if we only need small datasets, then storing data in an array is the best option.

Now, let’s assume we have a document that contains a property of type array. This array is named users and holds a few User objects. The User class is very simple, contains only two properties, and looks like this:

And this is the database structure:

A collection of applications, a random document, and an array

So our goal is to get in code the users array as a List<User>. To achieve that, we need to attach a listener on the document and use a get() call:

To actually get the values out of from users array, we are calling:

document.get("users")

And we cast the object to a List<Map<String, Object>>. So this object is actually a List of Maps. It’s true that we can iterate through the Map, get the data out and create the List<User> ourselves. But as DocumentSnapshot contains different flavors for the get() method, according to each data type, getString(), getLong(), getDate(), etc, it would have been very helpful if we also had a getList() method, but unfortunately we don’t. So something like this:

List<User> users = document.getList("users");

It’s not possible. So how can we still get a List<User>?

The simplest solution is to create another class that holds only a property of type List<User>. It looks like this:

And to directly get the list, only the following lines of code are needed:

That’s the simplest way you can get the content of an array as a list of custom objects.

Credit to Sam Stern 🙏

If you are using the Realtime Database and you’re looking for a similar solution, I have already written an article regarding the same topic:

If you wanna support me, please join me!

#BetterTogether 🔥

Firebase
Cloud Firestore
Android
Java
Recommended from ReadMedium