Firebase显示孩子是否存在 [英] Firebase show if child exists
问题描述
我正在使用 FirebaseIndexRecyclerAdapter ,并且尝试为以下用户显示帖子,因此我使用了这些方法自爆,但如果以下用户可以使用帖子ID,则会显示帖子:>
这是我的json:
"Following" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"pypD1SYZkbcYesk09WuMUY1AkTf1": true,
//I need to show this UID posts only but,
//When I add here post id it show the post
//Ex:"-KduqnVVczZf5uibQiZ-" : true,//Post Shows Up
},
"Posts" : {
"-KduqnVVczZf5uibQiZ-" : {
"Describe" : "gg",
"Time" : 14881230655,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Jone",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
},
"-Ke5gJ00CxbjhOuhzLIx" : {
"Describe" : "hajj",
"Time" : 1488318465,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
},
"-KeCuiFmUCpN19zwsTsR" : {
"Describe" : "a",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488439652,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
},
"-KeD2fBUQ09HVMrvAneb" : {
"Describe" : "ee",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488441999,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1904338270.jpg?alt=media&token=d200b7fc-15e7-4dc4-9913-59c377929e9e"
},
"-KeDfnMWiB7k_4J3FBgZ" : {
"Describe" : "f12g",
"Time" : 1488452517,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1"
"Username" : "Young",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
"-KeDfrw4inUQFtIDXJHp" : {
"Describe" : "fg",
"Time" : 1488452536,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1"
"Username" : "Young",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
}
}
这是我使用的方法:
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());
FirebaseIndexRecyclerAdapter<Getting_Posts,PostViewHolder> firebaseIndexRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class,R.layout.post_card_design,PostViewHolder.class,mDatabaseFriends,mDatabase)
{
@Override
protected void populateViewHolder(PostViewHolder viewHolder, Getting_Posts model, int position) {}};
要检查一个Fiven引用是否有孩子,应在回调函数中将dataSnapshot.exists()
或dataSnapshot.getChildrenCount() > 0
与给定的DataSnapshot
一起使用.
如果要检索id
等于关注者的帖子,则应修改数据库,在pur Posts
表中添加一个额外的级别,根据用户ID,该帖子已经在一起:
"Posts": {
"USER_ID_1": {
"post_id_1": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
},
"post_id_2": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
}
},
"USER_ID_2": {
"post_id_3": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
},
"post_id_4": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
}
}
要遍历所有用户内部的所有帖子(就像您遵循的是我发布的新数据模型一样),您可以按以下方式检索他们:
List<Getting_Posts> totalPosts = new ArrayList<>();
//Check if the dataSnapshot have childrens
if (dataSnapshot.hasChildren()){
//This for iterate for each USER_ID from your database
for (DataSnapshot users : dataSnapshot.getChildren()) {
//This second iterate for each post for each user
for (DataSnapshot posts : users.getChildren()) {
totalPosts.add(dataSnapshot.getValue(Getting_Posts.class));
}
}
}
return totalPosts;
您可以在下一篇文章或官方文档中阅读有关此内容的更多信息: https://www.airpair.com/firebase/posts/structuring-您的firebase数据
https://firebase.google.com/docs/database/web/structure-data?hl = zh-419
I am using FirebaseIndexRecyclerAdapter and I'am trying to show post for the following users so I have used these method blew but It show posts if the post id is available at the following users:
Here's my json:
"Following" : {
"ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : {
"pypD1SYZkbcYesk09WuMUY1AkTf1": true,
//I need to show this UID posts only but,
//When I add here post id it show the post
//Ex:"-KduqnVVczZf5uibQiZ-" : true,//Post Shows Up
},
"Posts" : {
"-KduqnVVczZf5uibQiZ-" : {
"Describe" : "gg",
"Time" : 14881230655,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Jone",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
},
"-Ke5gJ00CxbjhOuhzLIx" : {
"Describe" : "hajj",
"Time" : 1488318465,
"UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
},
"-KeCuiFmUCpN19zwsTsR" : {
"Describe" : "a",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488439652,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
},
"-KeD2fBUQ09HVMrvAneb" : {
"Describe" : "ee",
"Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
"Time" : 1488441999,
"UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
"Username" : "Dom",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1904338270.jpg?alt=media&token=d200b7fc-15e7-4dc4-9913-59c377929e9e"
},
"-KeDfnMWiB7k_4J3FBgZ" : {
"Describe" : "f12g",
"Time" : 1488452517,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1"
"Username" : "Young",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
"-KeDfrw4inUQFtIDXJHp" : {
"Describe" : "fg",
"Time" : 1488452536,
"UID" : "ORBBKfZAAUhqI1h7ojQDzYOjgkp1"
"Username" : "Young",
"Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1872832638.jpg?alt=media&token=80239844-a872-47e2-9689-92d368dd9452"
},
}
}
And here's my method that I use:
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());
FirebaseIndexRecyclerAdapter<Getting_Posts,PostViewHolder> firebaseIndexRecyclerAdapter = new FirebaseIndexRecyclerAdapter<Getting_Posts, PostViewHolder>(
Getting_Posts.class,R.layout.post_card_design,PostViewHolder.class,mDatabaseFriends,mDatabase)
{
@Override
protected void populateViewHolder(PostViewHolder viewHolder, Getting_Posts model, int position) {}};
To check if a fiven reference have childrens you should use dataSnapshot.exists()
or dataSnapshot.getChildrenCount() > 0
with the given DataSnapshot
on your callback.
If you want to retrieve the post which id
is equal to the followers you should modify your database adding an extra level to pur Posts
table where the post will be already together based on a user id:
"Posts": {
"USER_ID_1": {
"post_id_1": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
},
"post_id_2": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
}
},
"USER_ID_2": {
"post_id_3": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
},
"post_id_4": {
"Describe": "gg",
"Time": 14881230655,
"UID": "pypD1SYZkbcYesk09WuMUY1AkTf1",
"Username": "Jone"
}
}
To iterate looking to get all the posts inside each of your user's(just if you are following the new data model that I post you upside), you can retrieve them doing this:
List<Getting_Posts> totalPosts = new ArrayList<>();
//Check if the dataSnapshot have childrens
if (dataSnapshot.hasChildren()){
//This for iterate for each USER_ID from your database
for (DataSnapshot users : dataSnapshot.getChildren()) {
//This second iterate for each post for each user
for (DataSnapshot posts : users.getChildren()) {
totalPosts.add(dataSnapshot.getValue(Getting_Posts.class));
}
}
}
return totalPosts;
You can read more about this in the next post or in the oficial documentation: https://www.airpair.com/firebase/posts/structuring-your-firebase-data
https://firebase.google.com/docs/database/web/structure-data?hl=en-419
这篇关于Firebase显示孩子是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!