从Firebase查询获取密钥 [英] Getting keys from Firebase query
问题描述
我在从查询结果中获取密钥时遇到问题.这是我的Firebase数据库结构:
I am having problem getting keys from queried results. This is my Firebase Database structure:
{
"Users" : {
"7idb6ThWR8aqmnEHFao5GRCV1kI3" : {
"dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
"status" : "hi my name is erik",
"uid" : "7idb6ThWR8aqmnEHFao5GRCV1kI3",
"username" : "erik"
}
},
"posts" : {
"-KfsrGsY8TWb2wiMFtAq" : {
"dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
"image" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Post_Images%2Fcropped1354055061.jpg?alt=media&token=77fbc9ed-4356-43c1-b7bb-9563300a8b7b",
"small_title" : "tes",
"summary" : "tes",
"title" : "tes",
"uid" : "7idb6ThWR8aqmnEHFao5GRCV1kI3",
"username" : "erik"
}
}
}
我正在尝试获取KfsrGsY8TWb2wiMFtAq
,但是在下面使用此代码
I am trying to get KfsrGsY8TWb2wiMFtAq
, but using this code below
Query mThisUsersPosts;
public static final String TAG = "blah" ;
mDatabasePosts = FirebaseDatabase.getInstance().getReference().child("posts");
mThisUsersPosts = mDatabasePosts.orderByChild("uid").equalTo(mCurrentUser);
mThisUsersPosts.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String posts_key = dataSnapshot.getChildren().toString();
Log.d(TAG,posts_key);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我不明白为什么会得到随机回调,而不是得到结果,例如:
I don't understand why instead of getting the result I am getting random callbacks such as:
com.google.firebase.database.DataSnapshot$1@cfbcf64
com.google.firebase.database.DataSnapshot$1@961642a
com.google.firebase.database.DataSnapshot$1@6f0d191
非常感谢!
推荐答案
对Firebase数据库执行查询时,可能会有多个结果.因此,快照包含这些结果的列表.即使只有一个结果,快照也将包含一个结果的列表.因此,您需要遍历返回快照的子级以获取单个结果.
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result. So you need to iterate over the children of the returned snapshot to get the individual result(s).
mThisUsersPosts.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
System.out.println(child.getKey());
System.out.println(child.child("title").getValue());
}
}
请注意,我想知道您选择的数据结构是否适合您的应用程序.如果您的主要用例是显示特定用户的帖子,请考虑对数据进行如下建模:
Note that I wonder if the data structure you picked is right for your app though. If your main use-case is to display the posts for a specific user, consider modeling the data as such:
{
"Users" : {
"7idb6ThWR8aqmnEHFao5GRCV1kI3" : {
"dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
"status" : "hi my name is erik",
"uid" : "7idb6ThWR8aqmnEHFao5GRCV1kI3",
"username" : "erik"
}
},
"posts" : {
"7idb6ThWR8aqmnEHFao5GRCV1kI3": {
"-KfsrGsY8TWb2wiMFtAq" : {
"dPicture" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Profile_images%2Fcropped1465266876.jpg?alt=media&token=44f83fdf-935a-4b3c-8138-561dcba2fca7",
"image" : "https://firebasestorage.googleapis.com/v0/b/parkir-ngasal.appspot.com/o/Post_Images%2Fcropped1354055061.jpg?alt=media&token=77fbc9ed-4356-43c1-b7bb-9563300a8b7b",
"small_title" : "tes",
"summary" : "tes",
"title" : "tes",
"username" : "erik"
}
}
}
}
通过这种结构,您可以使用更简单/更快的结构为用户获取帖子:
With this structure you can get the posts for the user with the much simpler/faster construct:
mThisUsersPosts = mDatabasePosts.child(mCurrentUser);
mThisUsersPosts.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot child: dataSnapshot.getChildren()) {
System.out.println(child.getKey());
System.out.println(child.child("title").getValue());
}
}
这篇关于从Firebase查询获取密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!