在客户端,我将 RootNode 的所有子代的密钥存储在List<String> list中.通过遍历此列表,我向 RootNode 的每个子级添加了一个ValueEventListener. // Loop through the children of RootNode.for (final String keyOfChildNode: list) { // list contains the keys of all the children nodes of RootNode. Log.d(TAG, "Adding ValueEventListener to child node: " + keyOfChildNode); // Set a ValueEventListener to child node. RootNodeReference.child(keyOfChildNode).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // Datasnapshot of child has been retrieved. Log.d(TAG, "Datasnapshot retrieved for child node: " + keyOfChildNode); } @Override public void onCancelled(DatabaseError databaseError) { // Report error. } }); }这是控制台输出:Adding ValueEventListener to child node: keyChildNode001Adding ValueEventListener to child node: keyChildNode002Adding ValueEventListener to child node: keyChildNode003Adding ValueEventListener to child node: keyChildNode004Adding ValueEventListener to child node: keyChildNode005Adding ValueEventListener to child node: keyChildNode006Adding ValueEventListener to child node: keyChildNode007Adding ValueEventListener to child node: keyChildNode008Adding ValueEventListener to child node: keyChildNode009Adding ValueEventListener to child node: keyChildNode010Adding ValueEventListener to child node: keyChildNode011Adding ValueEventListener to child node: keyChildNode012Adding ValueEventListener to child node: keyChildNode013Adding ValueEventListener to child node: keyChildNode014Adding ValueEventListener to child node: keyChildNode015Adding ValueEventListener to child node: keyChildNode016Adding ValueEventListener to child node: keyChildNode017Adding ValueEventListener to child node: keyChildNode018Adding ValueEventListener to child node: keyChildNode019Adding ValueEventListener to child node: keyChildNode020Adding ValueEventListener to child node: keyChildNode021Adding ValueEventListener to child node: keyChildNode022Adding ValueEventListener to child node: keyChildNode023Adding ValueEventListener to child node: keyChildNode024Adding ValueEventListener to child node: keyChildNode025Adding ValueEventListener to child node: keyChildNode026Adding ValueEventListener to child node: keyChildNode027Adding ValueEventListener to child node: keyChildNode028Adding ValueEventListener to child node: keyChildNode029Adding ValueEventListener to child node: keyChildNode030Adding ValueEventListener to child node: keyChildNode031Adding ValueEventListener to child node: keyChildNode032Adding ValueEventListener to child node: keyChildNode033Adding ValueEventListener to child node: keyChildNode034Adding ValueEventListener to child node: keyChildNode035Adding ValueEventListener to child node: keyChildNode036Adding ValueEventListener to child node: keyChildNode037Adding ValueEventListener to child node: keyChildNode038Adding ValueEventListener to child node: keyChildNode039Adding ValueEventListener to child node: keyChildNode040Adding ValueEventListener to child node: keyChildNode041Adding ValueEventListener to child node: keyChildNode042Adding ValueEventListener to child node: keyChildNode043Adding ValueEventListener to child node: keyChildNode044Adding ValueEventListener to child node: keyChildNode045Adding ValueEventListener to child node: keyChildNode046Adding ValueEventListener to child node: keyChildNode047Adding ValueEventListener to child node: keyChildNode048Adding ValueEventListener to child node: keyChildNode049Adding ValueEventListener to child node: keyChildNode050Adding ValueEventListener to child node: keyChildNode051Adding ValueEventListener to child node: keyChildNode052Adding ValueEventListener to child node: keyChildNode053Adding ValueEventListener to child node: keyChildNode054Adding ValueEventListener to child node: keyChildNode055Adding ValueEventListener to child node: keyChildNode056Adding ValueEventListener to child node: keyChildNode057Adding ValueEventListener to child node: keyChildNode058Adding ValueEventListener to child node: keyChildNode059Adding ValueEventListener to child node: keyChildNode060Adding ValueEventListener to child node: keyChildNode061Adding ValueEventListener to child node: keyChildNode062Adding ValueEventListener to child node: keyChildNode063Adding ValueEventListener to child node: keyChildNode064Adding ValueEventListener to child node: keyChildNode065Adding ValueEventListener to child node: keyChildNode066Adding ValueEventListener to child node: keyChildNode067Adding ValueEventListener to child node: keyChildNode068Adding ValueEventListener to child node: keyChildNode069Adding ValueEventListener to child node: keyChildNode070Adding ValueEventListener to child node: keyChildNode071Adding ValueEventListener to child node: keyChildNode072Adding ValueEventListener to child node: keyChildNode073Adding ValueEventListener to child node: keyChildNode074Adding ValueEventListener to child node: keyChildNode075Adding ValueEventListener to child node: keyChildNode076Adding ValueEventListener to child node: keyChildNode077Adding ValueEventListener to child node: keyChildNode078Adding ValueEventListener to child node: keyChildNode079Adding ValueEventListener to child node: keyChildNode080Adding ValueEventListener to child node: keyChildNode081Adding ValueEventListener to child node: keyChildNode082Adding ValueEventListener to child node: keyChildNode083Adding ValueEventListener to child node: keyChildNode084Adding ValueEventListener to child node: keyChildNode085Adding ValueEventListener to child node: keyChildNode086Adding ValueEventListener to child node: keyChildNode087Adding ValueEventListener to child node: keyChildNode088Adding ValueEventListener to child node: keyChildNode089Adding ValueEventListener to child node: keyChildNode090Adding ValueEventListener to child node: keyChildNode091Adding ValueEventListener to child node: keyChildNode092Adding ValueEventListener to child node: keyChildNode093Adding ValueEventListener to child node: keyChildNode094Adding ValueEventListener to child node: keyChildNode095Adding ValueEventListener to child node: keyChildNode096Adding ValueEventListener to child node: keyChildNode097Adding ValueEventListener to child node: keyChildNode098Adding ValueEventListener to child node: keyChildNode099Adding ValueEventListener to child node: keyChildNode100Datasnapshot retrieved for child node: keyChildNode001Datasnapshot retrieved for child node: keyChildNode002Datasnapshot retrieved for child node: keyChildNode003Datasnapshot retrieved for child node: keyChildNode004Datasnapshot retrieved for child node: keyChildNode005Datasnapshot retrieved for child node: keyChildNode006Datasnapshot retrieved for child node: keyChildNode007Datasnapshot retrieved for child node: keyChildNode008Datasnapshot retrieved for child node: keyChildNode009Datasnapshot retrieved for child node: keyChildNode010Datasnapshot retrieved for child node: keyChildNode011Datasnapshot retrieved for child node: keyChildNode012Datasnapshot retrieved for child node: keyChildNode013Datasnapshot retrieved for child node: keyChildNode014Datasnapshot retrieved for child node: keyChildNode015Datasnapshot retrieved for child node: keyChildNode016Datasnapshot retrieved for child node: keyChildNode017Datasnapshot retrieved for child node: keyChildNode018Datasnapshot retrieved for child node: keyChildNode019Datasnapshot retrieved for child node: keyChildNode020Datasnapshot retrieved for child node: keyChildNode021Datasnapshot retrieved for child node: keyChildNode022Datasnapshot retrieved for child node: keyChildNode023Datasnapshot retrieved for child node: keyChildNode024Datasnapshot retrieved for child node: keyChildNode025Datasnapshot retrieved for child node: keyChildNode026Datasnapshot retrieved for child node: keyChildNode027Datasnapshot retrieved for child node: keyChildNode028Datasnapshot retrieved for child node: keyChildNode029Datasnapshot retrieved for child node: keyChildNode030Datasnapshot retrieved for child node: keyChildNode031Datasnapshot retrieved for child node: keyChildNode032Datasnapshot retrieved for child node: keyChildNode033Datasnapshot retrieved for child node: keyChildNode034Datasnapshot retrieved for child node: keyChildNode035Datasnapshot retrieved for child node: keyChildNode036Datasnapshot retrieved for child node: keyChildNode037Datasnapshot retrieved for child node: keyChildNode038Datasnapshot retrieved for child node: keyChildNode039Datasnapshot retrieved for child node: keyChildNode040Datasnapshot retrieved for child node: keyChildNode041Datasnapshot retrieved for child node: keyChildNode042Datasnapshot retrieved for child node: keyChildNode043Datasnapshot retrieved for child node: keyChildNode044Datasnapshot retrieved for child node: keyChildNode045Datasnapshot retrieved for child node: keyChildNode046Datasnapshot retrieved for child node: keyChildNode047Datasnapshot retrieved for child node: keyChildNode048Datasnapshot retrieved for child node: keyChildNode049Datasnapshot retrieved for child node: keyChildNode050Datasnapshot retrieved for child node: keyChildNode051Datasnapshot retrieved for child node: keyChildNode052Datasnapshot retrieved for child node: keyChildNode053Datasnapshot retrieved for child node: keyChildNode054Datasnapshot retrieved for child node: keyChildNode055Datasnapshot retrieved for child node: keyChildNode056Datasnapshot retrieved for child node: keyChildNode057Datasnapshot retrieved for child node: keyChildNode058Datasnapshot retrieved for child node: keyChildNode059Datasnapshot retrieved for child node: keyChildNode060Datasnapshot retrieved for child node: keyChildNode061Datasnapshot retrieved for child node: keyChildNode062Datasnapshot retrieved for child node: keyChildNode063Datasnapshot retrieved for child node: keyChildNode064Datasnapshot retrieved for child node: keyChildNode065Datasnapshot retrieved for child node: keyChildNode066Datasnapshot retrieved for child node: keyChildNode067Datasnapshot retrieved for child node: keyChildNode068Datasnapshot retrieved for child node: keyChildNode069Datasnapshot retrieved for child node: keyChildNode070Datasnapshot retrieved for child node: keyChildNode071Datasnapshot retrieved for child node: keyChildNode072Datasnapshot retrieved for child node: keyChildNode073Datasnapshot retrieved for child node: keyChildNode074Datasnapshot retrieved for child node: keyChildNode075Datasnapshot retrieved for child node: keyChildNode076Datasnapshot retrieved for child node: keyChildNode077Datasnapshot retrieved for child node: keyChildNode078Datasnapshot retrieved for child node: keyChildNode079Datasnapshot retrieved for child node: keyChildNode080Datasnapshot retrieved for child node: keyChildNode081Datasnapshot retrieved for child node: keyChildNode082Datasnapshot retrieved for child node: keyChildNode083Datasnapshot retrieved for child node: keyChildNode084Datasnapshot retrieved for child node: keyChildNode085Datasnapshot retrieved for child node: keyChildNode086Datasnapshot retrieved for child node: keyChildNode087Datasnapshot retrieved for child node: keyChildNode088Datasnapshot retrieved for child node: keyChildNode089Datasnapshot retrieved for child node: keyChildNode090Datasnapshot retrieved for child node: keyChildNode091Datasnapshot retrieved for child node: keyChildNode092Datasnapshot retrieved for child node: keyChildNode093Datasnapshot retrieved for child node: keyChildNode094Datasnapshot retrieved for child node: keyChildNode095Datasnapshot retrieved for child node: keyChildNode096Datasnapshot retrieved for child node: keyChildNode097Datasnapshot retrieved for child node: keyChildNode098Datasnapshot retrieved for child node: keyChildNode099Datasnapshot retrieved for child node: keyChildNode100让我感到惊讶的是:由于添加到子引用中的ValueEventListeners本质上是异步的,因此我希望快照将以随机顺序进行检索(不一定以与ValueEventListeners相同的顺序进行检索).添加).但是控制台输出显示快照是按照ValueEventListeners添加到每个子节点的顺序检索的.以有序"方式检索快照是否正常?如果是,鉴于侦听器的异步特性,怎么可能?预先谢谢你.解决方案这确实是预期的行为. Firebase数据库客户端通过单个套接字连接与其服务器进行通信.此套接字用于客户端和数据库服务器之间的所有通信.当您请求100条记录时,所有请求均以拨打电话的顺序通过此单个连接发送.然后,服务器以相同的顺序执行请求,并以相同的顺序返回结果.这称为管道传输:通过单个连接发送多个请求并按顺序返回结果.在此之前,我已经回答了有关此问题的更多信息:通过使用查询而不是重复观察单个事件,来加快我的社交网络应用的帖子获取速度 I'm building an Android app that heavily uses Firebase's Database.In the Database, I have a node called RootNode which contains 100 children nodes. Each child of RootNode has its own children (from 30 to 60 each), each with its own key-value properties (so, we're talking a substantial amount of data per child node).Client-side, I have the keys of all the children of RootNode stored in List<String> list. By looping through this list, I'm adding a ValueEventListener to each child of RootNode. // Loop through the children of RootNode.for (final String keyOfChildNode: list) { // list contains the keys of all the children nodes of RootNode. Log.d(TAG, "Adding ValueEventListener to child node: " + keyOfChildNode); // Set a ValueEventListener to child node. RootNodeReference.child(keyOfChildNode).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // Datasnapshot of child has been retrieved. Log.d(TAG, "Datasnapshot retrieved for child node: " + keyOfChildNode); } @Override public void onCancelled(DatabaseError databaseError) { // Report error. } }); }And this is the console output:Adding ValueEventListener to child node: keyChildNode001Adding ValueEventListener to child node: keyChildNode002Adding ValueEventListener to child node: keyChildNode003Adding ValueEventListener to child node: keyChildNode004Adding ValueEventListener to child node: keyChildNode005Adding ValueEventListener to child node: keyChildNode006Adding ValueEventListener to child node: keyChildNode007Adding ValueEventListener to child node: keyChildNode008Adding ValueEventListener to child node: keyChildNode009Adding ValueEventListener to child node: keyChildNode010Adding ValueEventListener to child node: keyChildNode011Adding ValueEventListener to child node: keyChildNode012Adding ValueEventListener to child node: keyChildNode013Adding ValueEventListener to child node: keyChildNode014Adding ValueEventListener to child node: keyChildNode015Adding ValueEventListener to child node: keyChildNode016Adding ValueEventListener to child node: keyChildNode017Adding ValueEventListener to child node: keyChildNode018Adding ValueEventListener to child node: keyChildNode019Adding ValueEventListener to child node: keyChildNode020Adding ValueEventListener to child node: keyChildNode021Adding ValueEventListener to child node: keyChildNode022Adding ValueEventListener to child node: keyChildNode023Adding ValueEventListener to child node: keyChildNode024Adding ValueEventListener to child node: keyChildNode025Adding ValueEventListener to child node: keyChildNode026Adding ValueEventListener to child node: keyChildNode027Adding ValueEventListener to child node: keyChildNode028Adding ValueEventListener to child node: keyChildNode029Adding ValueEventListener to child node: keyChildNode030Adding ValueEventListener to child node: keyChildNode031Adding ValueEventListener to child node: keyChildNode032Adding ValueEventListener to child node: keyChildNode033Adding ValueEventListener to child node: keyChildNode034Adding ValueEventListener to child node: keyChildNode035Adding ValueEventListener to child node: keyChildNode036Adding ValueEventListener to child node: keyChildNode037Adding ValueEventListener to child node: keyChildNode038Adding ValueEventListener to child node: keyChildNode039Adding ValueEventListener to child node: keyChildNode040Adding ValueEventListener to child node: keyChildNode041Adding ValueEventListener to child node: keyChildNode042Adding ValueEventListener to child node: keyChildNode043Adding ValueEventListener to child node: keyChildNode044Adding ValueEventListener to child node: keyChildNode045Adding ValueEventListener to child node: keyChildNode046Adding ValueEventListener to child node: keyChildNode047Adding ValueEventListener to child node: keyChildNode048Adding ValueEventListener to child node: keyChildNode049Adding ValueEventListener to child node: keyChildNode050Adding ValueEventListener to child node: keyChildNode051Adding ValueEventListener to child node: keyChildNode052Adding ValueEventListener to child node: keyChildNode053Adding ValueEventListener to child node: keyChildNode054Adding ValueEventListener to child node: keyChildNode055Adding ValueEventListener to child node: keyChildNode056Adding ValueEventListener to child node: keyChildNode057Adding ValueEventListener to child node: keyChildNode058Adding ValueEventListener to child node: keyChildNode059Adding ValueEventListener to child node: keyChildNode060Adding ValueEventListener to child node: keyChildNode061Adding ValueEventListener to child node: keyChildNode062Adding ValueEventListener to child node: keyChildNode063Adding ValueEventListener to child node: keyChildNode064Adding ValueEventListener to child node: keyChildNode065Adding ValueEventListener to child node: keyChildNode066Adding ValueEventListener to child node: keyChildNode067Adding ValueEventListener to child node: keyChildNode068Adding ValueEventListener to child node: keyChildNode069Adding ValueEventListener to child node: keyChildNode070Adding ValueEventListener to child node: keyChildNode071Adding ValueEventListener to child node: keyChildNode072Adding ValueEventListener to child node: keyChildNode073Adding ValueEventListener to child node: keyChildNode074Adding ValueEventListener to child node: keyChildNode075Adding ValueEventListener to child node: keyChildNode076Adding ValueEventListener to child node: keyChildNode077Adding ValueEventListener to child node: keyChildNode078Adding ValueEventListener to child node: keyChildNode079Adding ValueEventListener to child node: keyChildNode080Adding ValueEventListener to child node: keyChildNode081Adding ValueEventListener to child node: keyChildNode082Adding ValueEventListener to child node: keyChildNode083Adding ValueEventListener to child node: keyChildNode084Adding ValueEventListener to child node: keyChildNode085Adding ValueEventListener to child node: keyChildNode086Adding ValueEventListener to child node: keyChildNode087Adding ValueEventListener to child node: keyChildNode088Adding ValueEventListener to child node: keyChildNode089Adding ValueEventListener to child node: keyChildNode090Adding ValueEventListener to child node: keyChildNode091Adding ValueEventListener to child node: keyChildNode092Adding ValueEventListener to child node: keyChildNode093Adding ValueEventListener to child node: keyChildNode094Adding ValueEventListener to child node: keyChildNode095Adding ValueEventListener to child node: keyChildNode096Adding ValueEventListener to child node: keyChildNode097Adding ValueEventListener to child node: keyChildNode098Adding ValueEventListener to child node: keyChildNode099Adding ValueEventListener to child node: keyChildNode100Datasnapshot retrieved for child node: keyChildNode001Datasnapshot retrieved for child node: keyChildNode002Datasnapshot retrieved for child node: keyChildNode003Datasnapshot retrieved for child node: keyChildNode004Datasnapshot retrieved for child node: keyChildNode005Datasnapshot retrieved for child node: keyChildNode006Datasnapshot retrieved for child node: keyChildNode007Datasnapshot retrieved for child node: keyChildNode008Datasnapshot retrieved for child node: keyChildNode009Datasnapshot retrieved for child node: keyChildNode010Datasnapshot retrieved for child node: keyChildNode011Datasnapshot retrieved for child node: keyChildNode012Datasnapshot retrieved for child node: keyChildNode013Datasnapshot retrieved for child node: keyChildNode014Datasnapshot retrieved for child node: keyChildNode015Datasnapshot retrieved for child node: keyChildNode016Datasnapshot retrieved for child node: keyChildNode017Datasnapshot retrieved for child node: keyChildNode018Datasnapshot retrieved for child node: keyChildNode019Datasnapshot retrieved for child node: keyChildNode020Datasnapshot retrieved for child node: keyChildNode021Datasnapshot retrieved for child node: keyChildNode022Datasnapshot retrieved for child node: keyChildNode023Datasnapshot retrieved for child node: keyChildNode024Datasnapshot retrieved for child node: keyChildNode025Datasnapshot retrieved for child node: keyChildNode026Datasnapshot retrieved for child node: keyChildNode027Datasnapshot retrieved for child node: keyChildNode028Datasnapshot retrieved for child node: keyChildNode029Datasnapshot retrieved for child node: keyChildNode030Datasnapshot retrieved for child node: keyChildNode031Datasnapshot retrieved for child node: keyChildNode032Datasnapshot retrieved for child node: keyChildNode033Datasnapshot retrieved for child node: keyChildNode034Datasnapshot retrieved for child node: keyChildNode035Datasnapshot retrieved for child node: keyChildNode036Datasnapshot retrieved for child node: keyChildNode037Datasnapshot retrieved for child node: keyChildNode038Datasnapshot retrieved for child node: keyChildNode039Datasnapshot retrieved for child node: keyChildNode040Datasnapshot retrieved for child node: keyChildNode041Datasnapshot retrieved for child node: keyChildNode042Datasnapshot retrieved for child node: keyChildNode043Datasnapshot retrieved for child node: keyChildNode044Datasnapshot retrieved for child node: keyChildNode045Datasnapshot retrieved for child node: keyChildNode046Datasnapshot retrieved for child node: keyChildNode047Datasnapshot retrieved for child node: keyChildNode048Datasnapshot retrieved for child node: keyChildNode049Datasnapshot retrieved for child node: keyChildNode050Datasnapshot retrieved for child node: keyChildNode051Datasnapshot retrieved for child node: keyChildNode052Datasnapshot retrieved for child node: keyChildNode053Datasnapshot retrieved for child node: keyChildNode054Datasnapshot retrieved for child node: keyChildNode055Datasnapshot retrieved for child node: keyChildNode056Datasnapshot retrieved for child node: keyChildNode057Datasnapshot retrieved for child node: keyChildNode058Datasnapshot retrieved for child node: keyChildNode059Datasnapshot retrieved for child node: keyChildNode060Datasnapshot retrieved for child node: keyChildNode061Datasnapshot retrieved for child node: keyChildNode062Datasnapshot retrieved for child node: keyChildNode063Datasnapshot retrieved for child node: keyChildNode064Datasnapshot retrieved for child node: keyChildNode065Datasnapshot retrieved for child node: keyChildNode066Datasnapshot retrieved for child node: keyChildNode067Datasnapshot retrieved for child node: keyChildNode068Datasnapshot retrieved for child node: keyChildNode069Datasnapshot retrieved for child node: keyChildNode070Datasnapshot retrieved for child node: keyChildNode071Datasnapshot retrieved for child node: keyChildNode072Datasnapshot retrieved for child node: keyChildNode073Datasnapshot retrieved for child node: keyChildNode074Datasnapshot retrieved for child node: keyChildNode075Datasnapshot retrieved for child node: keyChildNode076Datasnapshot retrieved for child node: keyChildNode077Datasnapshot retrieved for child node: keyChildNode078Datasnapshot retrieved for child node: keyChildNode079Datasnapshot retrieved for child node: keyChildNode080Datasnapshot retrieved for child node: keyChildNode081Datasnapshot retrieved for child node: keyChildNode082Datasnapshot retrieved for child node: keyChildNode083Datasnapshot retrieved for child node: keyChildNode084Datasnapshot retrieved for child node: keyChildNode085Datasnapshot retrieved for child node: keyChildNode086Datasnapshot retrieved for child node: keyChildNode087Datasnapshot retrieved for child node: keyChildNode088Datasnapshot retrieved for child node: keyChildNode089Datasnapshot retrieved for child node: keyChildNode090Datasnapshot retrieved for child node: keyChildNode091Datasnapshot retrieved for child node: keyChildNode092Datasnapshot retrieved for child node: keyChildNode093Datasnapshot retrieved for child node: keyChildNode094Datasnapshot retrieved for child node: keyChildNode095Datasnapshot retrieved for child node: keyChildNode096Datasnapshot retrieved for child node: keyChildNode097Datasnapshot retrieved for child node: keyChildNode098Datasnapshot retrieved for child node: keyChildNode099Datasnapshot retrieved for child node: keyChildNode100One thing surprised me: since the ValueEventListeners added to the child references are asynchronous in nature, I expected that the snapshots would be retrieved in random order (not necessarily in the same order that the ValueEventListeners were added). But the console output shows that the snapshots are retrieved in the order that the ValueEventListeners were added to each child node.Is it normal that the snapshots were retrieved in this 'orderly' fashion? If yes, how is it possible given the asynchronous nature of the listeners? Thank you in advance. 解决方案 This is indeed the expected behavior.The Firebase database client communicates with its server through a single socket connection. This socket is used for all communication between the client and the database server.When you're requesting the 100 records, the requests are all sent through this single connection in the order in which you make the calls. The server then executes the requests in the same order and returns the results in the same order.This is known as pipelining: sending multiple requests over a single connection and returning the results in order. I've answered a bit more about it before here: Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly 这篇关于Android Firebase数据库-多个ValueEventListeners的快照检索顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 10:59