Skip to content Skip to sidebar Skip to footer

Firebase "parcelable Encountered Ioexception Writing Serializable Object" With Object Containing Arraylist Of Documentreference

I'm trying to pass to another activity a User object containing an Arraylist of Firestore DocumentReference and when I start the activity I got this exception. I'm not using Parcel

Solution 1:

You get that error because DocumentReference class does not implement Parceleable nor Serializable.

If you need to serialize a DocumentReference object you need to use its getPath() method to get a String that describes the document's location in the database. To deserialize that path String back into a DocumentReference object, use the following lines of code:

FirebaseFirestorerootRef= FirebaseFirestore.getInstance();
DocumentReferenceref= rootRef.document(path);

Please see official documentation for FirebaseFirestore.getInstance().document(path) method.

Solution 2:

The accepted answer solved the problem for me. I just want to share my implementation for couple of methods to handle the Parcel on the fly! and still have the array as ArrayList<DocumentReference>.

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

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

And in the Parcel implementation ..

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

@OverridepublicvoidwriteToParcel(Parcel dest, int flags) {
    ...
    dest.writeStringList(docRefToString(this.products));
    ...
}

Post a Comment for "Firebase "parcelable Encountered Ioexception Writing Serializable Object" With Object Containing Arraylist Of Documentreference"