Skip to content

Instantly share code, notes, and snippets.

@benhaxe
Last active November 22, 2017 16:56
Show Gist options
  • Save benhaxe/1d26726a8e9aa3bd284ab29ae83e8144 to your computer and use it in GitHub Desktop.
Save benhaxe/1d26726a8e9aa3bd284ab29ae83e8144 to your computer and use it in GitHub Desktop.
Still on firebase database, the code shows how to update 2 nodes in the database structure. Cloned from the firebase post app.
/*Please feel free to check the firbase real time databse documentation */
public class NewPost(String userId, String username, String title, String body){
// Get[key] used to store the post at the node ["posts"]
// [push] create the post at node["posts"]
String key = mDatabase.child("posts").push.getKey();
Post post = new Post(userId, username, title, body);
//[Map] is a key value object in java
// follow the link here: [https://goo.gl/e2XHLa] you should see the code where the method[toMap] is declared.
Map<String, Object> postValues = post.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/posts/" + key, postValues);
childUpdates.put("/users-posts/" + userId + "/" + key, postValues);
//The method[updateChildren] is used to update the child at a node.
// Let me explain the dunction of push here
/*
*The [push] method generate a unique key everytime a child is added
*The [getKey] method is used to get the key that is generated by the [push]method and this key will serve as the post id.
*The methd[push()] first create the post[in postValues] at the node ["posts"] for all users at /posts/$postid.
*and simultaneously send that same post to users @/user-posts/$userid/$postid
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment