我无法设置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) {
}
});
我看不出我做错了什么。我设置了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 :(得分: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);
}