2016-07-26 59 views
6

我無法設置我的Firebase對象的優先級。以下是我的嘗試:通過優先級(從低到高)檢索Firebase數據不起作用?

第一個是我如何將Post對象保存到Firebase,從而保持優先級低。我會做0 - date.getTime(),因爲我知道,火力地堡的文件說,當務之急是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) { 

     } 
    }); 

我看不到我在做什麼錯。我從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" 
    } 
    }, 
+0

你已經包括了JSON樹在你的問題的照片。請用實際的JSON替換爲文本,您可以通過點擊Firebase數據庫控制檯中的導出按鈕輕鬆獲取。將JSON作爲文本可以搜索,使我們能夠輕鬆使用它來測試您的實際數據,並將其用於我們的答案中,並且通常只是一件好事。在你的具體情況下,確保'.priority'也​​在JSON中,因爲這對你的問題非常重要。 –

+0

@FrankvanPuffelen嗨,我用我的數據庫應該是什麼樣子的照片替換了我的數據庫的照片。另外,我意識到'.priorty'不會自動包含在JSON中,我是否必須自己手動包含這個?有什麼更簡單的方法可以展示這個嗎? – user1871869

+0

您可以通過調用'DataSnapshot.exportVal()'獲得具有*優先級的JSON *。 –

回答

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); 
}