Skip to content Skip to sidebar Skip to footer

How To Manage Firebase Database Keys?

When using firebase (or any database that aggregates data basing on ids) I nearly always have to keep track of a key of a given value. For example let's assume I have Location cla

Solution 1:

What you call ridiculous actually looks quite normal to me.

Alternatively you can include the key in the POJO and annotate it with @Exclude to exclude it from the serialization.

Solution 2:

Follow up on @FrankvanPuffelen great answer, do what you want with the below pushkey

Read and Write Data on Android

privatevoidwriteNewPost(String userId, String username, String title, String body) {
    // Create new post at /user-posts/$userid/$postid and at// /posts/$postid simultaneouslyString key = mDatabase.child("posts").push().getKey();
    Post post = newPost(userId, username, title, body);
    Map<String, Object> postValues = post.toMap();

    Map<String, Object> childUpdates = newHashMap<>();
    childUpdates.put("/posts/" + key, postValues);
    childUpdates.put("/user-posts/" + userId + "/" + key, postValues);

    mDatabase.updateChildren(childUpdates);
}

Post a Comment for "How To Manage Firebase Database Keys?"