问题描述
我想从我的Firebase数据结构中获取最后一个孩子,在该数据结构中我只知道接收到的孩子的第一个孩子的引用
i want to get last child from my firebase data structure in which i only know the reference of received and first child of it
我尝试了这个,但是它将返回所有孩子,但是我只需要在查询limiTolast(1)中使用的最后一个孩子
i try this one but it will return all child but i need only last one that use in query limiTolast(1) like this
DatabaseReference users = mDatabase.child("received");
// DatabaseReference receiver = users.child(firebaseUser.getUid());
final DatabaseReference receiver =
users.child("GTjrWgpKjoeXUt4JdBJTYP1JkVT2");
receiver.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, "key count=" + postSnapshot.getKey());
for (DataSnapshot sender: postSnapshot.getChildren()) {
Log.d(TAG, "sender key count=" + sender.getKey());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.e("SHAN " ,databaseError.getMessage());
}
});
推荐答案
如果知道父节点(received/GTjrWgpKjoeXUt4JdBJTYP1JkVT2/fnBOM...
)的完整路径,则只能使用limitToLast(1)
获取该位置下的最后一个子节点:
If you know the complete path of the parent node (received/GTjrWgpKjoeXUt4JdBJTYP1JkVT2/fnBOM...
), you can get only the last child node under that location with limitToLast(1)
:
DatabaseReference ref = users.child("GTjrWgpKjoeXUt4JdBJTYP1JkVT2/fnBOM...`");
ref.orderByKey().limitToLast(1).addChildEventListener(...
如果您不知道父节点的完整路径,则无法检索子节点的子集.
If you don't know the complete path to the parent node, there is no way to retrieve a subset of the child nodes.
这篇关于我需要获取我的Firebase数据库的最后一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!