Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Firebase Data by priority (lowest to highest) not working?

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

The first is how I save the Post object to Firebase, keeping the priority low. I would do 0 - date.getTime() because I know that Firebase's documentation says 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's 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 can't see what I am doing wrong. I set the priority from smallest to largest and also made sure that when retrieving the Post I was using the orderByPriority() API to do so. I print out the priority of my Post object but it ends up being null always. If anyone could point me in the right direction that'd be great. Thanks!

EDIT: Here is a sample of what my data looks like. the .prioirty isn't showing up in my data... I'm not sure why. though...

  "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"
    }
  },
like image 600
user1871869 Avatar asked Jul 26 '16 06:07

user1871869


People also ask

How do I speed up firestore query?

How to make this faster. The best way to fix this issue is to make sure you're not transferring down more data than you need. One simple option is to add limits to your queries.

Why do we try to keep Firebase data flat?

Because Realtime Database retrieves the data from a path's child nodes as well as the path, it makes sense to keep your data structure as flat as possible. This way, you can selectively retrieve the data you need, without also downloading unnecessary data to clients.


1 Answers

I think you should try 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's path to be true
   currentUserPath.child("/posts/" + postsRef.getKey()).setValue(true);
}
like image 169
Sahaj Rana Avatar answered Oct 27 '22 01:10

Sahaj Rana