Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

192
Vistas
Firestore DocumentReference Serialization

I have a pojo which maps a firestore document. That document contains references to other documents of other collection.

class Game(var local: String? = null, var visitor: String? = null,
        var events: MutableList<DocumentReference> = mutableListOf(), var date: Date? = null): Serializable 

The problem is that DocumentReference isn't serializable. I've read that I can save the path as String and then create the object myself(FirebaseFirestore.getInstance().document(path)), but then in the firestore is no longer a field of type Reference.

So my question, Is this the good approach? Also, Does it matter that the field is a String rather than a Reference?

Regards, Diego

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

So my question is, is this the good approach?

Yes it is. The most important part in a DocumenetReference is the string path. If you serialize that, you can reconstitute a DocumentReference object, very easy.

As the conclusion, if you need to serialize a DocumentReference object, use DocumentReference's getPath() method to get a literal String that represents the document's location in the database. To deserialize that path String back into a DocumentReference object, simply use FirebaseFirestore.getInstance().document(path).

Edit:

This implies that the field in the database is now a String instead of Reference.

That's right.

Does it matter? I suppose that being a Reference must have some type of advantage.

Yes, since now the property is of type a String, you cannot take advantage of what DocumentReference class provides anymore. But this is the workaround that can help you achieve what you want. String class is serializable while DocumentReference's is not.

over 4 years ago · Santiago Trujillo Denunciar

0

I've an elaborated answer here. The code is

private ArrayList<String> docRefToString(ArrayList<DocumentReference> products) {
    ArrayList<String> newProducts = new ArrayList<String>();
    for (DocumentReference product : products) {
        newProducts.add(product.getPath());
    }
    return newProducts;
}

private ArrayList<DocumentReference> docStringToDoc(ArrayList<String> products) {
    ArrayList<DocumentReference> newProducts = new ArrayList<DocumentReference>();
    for (String product : products) {
        newProducts.add(FirebaseFirestore.getInstance().document(product));
    }
    return newProducts;
}

And in the Parcel implementation ..

private Request(Parcel in) {
    ...
    ArrayList<String> strProducts = in.createStringArrayList();
    this.products = docStringToDoc(strProducts);
    ...
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    ...
    dest.writeStringList(docRefToString(this.products));
    ...
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda