Getting Firebase data by priority (lowest to highest) doesn't work?

I'm having trouble setting the priority of Firebase objects. Following is my attempt:

First, I save the Post object for Firebase, while maintaining a low priority. I would do 0 - date.getTime() , because I know that the Firebase documentation states that priority is sorted numerically by priority, small to large. This is what I did then:

 private Firebase firebaseRef = new Firebase("https://<DataBase>.com/"); private Firebase currentUserPath = firebaseRef.child("users/" + userId); public void savePostToFirebase(final Post post, Location location) { //Firstly, we need to keep track of all posts so we put them into a posts path. final Firebase postsRef = firebaseRef.child("posts").push(); String postId = postsRef.getKey(); post.setId(postId); Date date = new Date(); postsRef.setValue(post, 0 - date.getTime()); //Next, we need to set that post in your own post path to be true currentUserPath.child("/posts/" + postsRef.getKey()).setValue(true); } public void getPosts(final Boolean loggedIn, final Activity activity) { final Firebase postsRef = firebaseRef.child("/posts"); Firebase linkRef = currentUserPath.child("/posts"); linkRef.orderByPriority().addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { postsRef.child(dataSnapshot.getKey()).orderByPriority().addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { System.out.println("Post Priority " + dataSnapshot.getPriority()); Post post = dataSnapshot.getValue(Post.class); application.getPostsAdapter().getPosts().add(post); } @Override public void onCancelled(FirebaseError firebaseError) { } }); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s) { } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { } @Override public void onCancelled(FirebaseError firebaseError) { } }); 

I do not see what I am doing wrong. I set the priority from smallest to largest , and also made sure that when extracting the Post I used the orderByPriority() API to do this. I am printing the priority of my Post object, but it ends up as null . If someone could point me in the right direction, that would be great. Thanks!

EDIT: Here is an example of what my data looks like ... prioirty doesn't appear in my data ... I'm not sure why. although...

  "posts" : { "-KN_oJAgTNJHwJbqY3qk" : { "id" : "-KN_oJAgTNJHwJbqY3qk", "name" : "Stephen", "numComments" : 1, "posterUserId" : "10206287838487450", "privacy" : "Public", "status" : "first post", "timeStamp" : "07/25/2016 11:08:05 PM", "title" : "first post" }, "-KN_oN9_Xmw5ULnBRYM7" : { "id" : "-KN_oN9_Xmw5ULnBRYM7", "name" : "Stephen", "numComments" : 0, "posterUserId" : "10206287838487450", "privacy" : "Public", "status" : "second post", "timeStamp" : "07/25/2016 11:08:21 PM", "title" : "second post" }, "-KN_obYGug9Tdrzufbqc" : { "id" : "-KN_obYGug9Tdrzufbqc", "name" : "Stephen", "numComments" : 0, "posterUserId" : "10206287838487450", "privacy" : "Public", "status" : "this post", "timeStamp" : "07/25/2016 11:09:24 PM", "title" : "third party" } }, 
+6
source share
1 answer

I think you should try it like this.

 public void savePostToFirebase(final Post post, Location location) { //Firstly, we need to keep track of all posts so we put them into a posts path. String postId = postsRef.getKey(); post.setId(postId); Date date = new Date(); firebaseRef.child("posts").setValue(post, 0 - date.getTime()); //Next, we need to set that post in your own post path to be true currentUserPath.child("/posts/" + postsRef.getKey()).setValue(true); } 
+1
source

All Articles