按优先级(从最低到最高)检索Firebase数据不起作用?

use*_*869 6 android firebase firebase-realtime-database

我在设置Firebase对象的优先级时遇到问题.以下是我的尝试:

第一个是我如何将Post对象保存到Firebase,保持优先级低.我会这样做,0 - date.getTime()因为我知道Firebase的文档说优先级是sorted numerically by priority, small to large.这就是我所做的:

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) {

        }
    });
Run Code Online (Sandbox Code Playgroud)

我看不出我做错了什么.我设置优先级,smallest to largest并确保在检索Post我使用orderByPriority()API时这样做.我打印出我的Post对象的优先级,但最终null总是.如果有人能指出我正确的方向,那将是伟大的.谢谢!

编辑:这是我的数据的样本..prioirty没有显示在我的数据中...我不知道为什么.虽然...

  "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"
    }
  },
Run Code Online (Sandbox Code Playgroud)

Sah*_*ana 1

我想你应该这样尝试..

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);
}
Run Code Online (Sandbox Code Playgroud)