BrunoTS
BrunoTS

Reputation: 171

Firebase saving objects with maps

I do not know why but I can not save my Object to FirebaseDatabase. Can someone help me?

My object:

public class ChatEntity {

    public static final String ENTITY_IDENTIFIER = "chats";

    private String id;
    private String chatTitle;
    private Map<String, ProfileEntity> users;
    private Map<String, ProfileEntity> administratorsUser;
    private Bitmap chatIcon;

    ... Getters and Setters ...

}

I'm saving this with:

FirebaseDatabase.getInstance().getReference(ChatEntity.ENTITY_IDENTIFIER).child(chat.getId()).setValue(chat);

And only this is saved:

{
  "1521122180142&Teste&tduWYxVHRVPVIx6Sv4p8fNwKKJi2" : {
    "chatTitle" : "Teste",
    "id" : "1521122180142&Teste&tduWYxVHRVPVIx6Sv4p8fNwKKJi2"
  }
}

The field chatIcon was null, so it's ok. But the two Maps has 1 and 2 entrys, and don't getting saved.

How can I save it?

Upvotes: 1

Views: 581

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

As per official documentation, the Bitmap is not a supported data type in Firebase Realtime database. Remember, as a general rule, I'd say, never use base64. You don't really need to base64-encode your image. If you want to store images, don't use Firebase Realtime database, use Firebase Storage.

In order to have your database populated with users and administratorsUser, change the type from those maps with ProfileEntity objects. If you want to use maps, you can use to set/update them directly on a DatabaseReference object.

Upvotes: 0

Alejandro Bertinelli
Alejandro Bertinelli

Reputation: 545

As you might see, the Firebase database only takes the String objects that you try to save. Any other object is not guaranteed to be saved, specially the Bitmap one. If you want to save a Bitmap:

  • Use Firebase Storage
  • Get the Base64 String from the Bitmap.

To save the nested HashMap, you might want to call the database several times (one for each Map), or to convert the Maps' values into Strings with a / for each time you are editing a child (although both of them are very messy).

Upvotes: 1

Related Questions