2017-01-10 20 views
2

Firebaseのデータベースを大量に使用するAndroidアプリケーションを構築しています。FirebaseデータベースAndroid - 複数のValueEventListenersからのスナップショット取得オーダー

データベースには、という名前のノードがあり、100個の子ノードを含むRootNodeがあります。 RootNodeの各子は、独自のキー値プロパティを持つ独自の子(30から60まで)を持っています(したがって、子ノードごとに相当量のデータを扱っています)。

クライアント側では、のすべての子のキーがあります。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: keyChildNode001 
Adding ValueEventListener to child node: keyChildNode002 
Adding ValueEventListener to child node: keyChildNode003 
Adding ValueEventListener to child node: keyChildNode004 
Adding ValueEventListener to child node: keyChildNode005 
Adding ValueEventListener to child node: keyChildNode006 
Adding ValueEventListener to child node: keyChildNode007 
Adding ValueEventListener to child node: keyChildNode008 
Adding ValueEventListener to child node: keyChildNode009 
Adding ValueEventListener to child node: keyChildNode010 
Adding ValueEventListener to child node: keyChildNode011 
Adding ValueEventListener to child node: keyChildNode012 
Adding ValueEventListener to child node: keyChildNode013 
Adding ValueEventListener to child node: keyChildNode014 
Adding ValueEventListener to child node: keyChildNode015 
Adding ValueEventListener to child node: keyChildNode016 
Adding ValueEventListener to child node: keyChildNode017 
Adding ValueEventListener to child node: keyChildNode018 
Adding ValueEventListener to child node: keyChildNode019 
Adding ValueEventListener to child node: keyChildNode020 
Adding ValueEventListener to child node: keyChildNode021 
Adding ValueEventListener to child node: keyChildNode022 
Adding ValueEventListener to child node: keyChildNode023 
Adding ValueEventListener to child node: keyChildNode024 
Adding ValueEventListener to child node: keyChildNode025 
Adding ValueEventListener to child node: keyChildNode026 
Adding ValueEventListener to child node: keyChildNode027 
Adding ValueEventListener to child node: keyChildNode028 
Adding ValueEventListener to child node: keyChildNode029 
Adding ValueEventListener to child node: keyChildNode030 
Adding ValueEventListener to child node: keyChildNode031 
Adding ValueEventListener to child node: keyChildNode032 
Adding ValueEventListener to child node: keyChildNode033 
Adding ValueEventListener to child node: keyChildNode034 
Adding ValueEventListener to child node: keyChildNode035 
Adding ValueEventListener to child node: keyChildNode036 
Adding ValueEventListener to child node: keyChildNode037 
Adding ValueEventListener to child node: keyChildNode038 
Adding ValueEventListener to child node: keyChildNode039 
Adding ValueEventListener to child node: keyChildNode040 
Adding ValueEventListener to child node: keyChildNode041 
Adding ValueEventListener to child node: keyChildNode042 
Adding ValueEventListener to child node: keyChildNode043 
Adding ValueEventListener to child node: keyChildNode044 
Adding ValueEventListener to child node: keyChildNode045 
Adding ValueEventListener to child node: keyChildNode046 
Adding ValueEventListener to child node: keyChildNode047 
Adding ValueEventListener to child node: keyChildNode048 
Adding ValueEventListener to child node: keyChildNode049 
Adding ValueEventListener to child node: keyChildNode050 
Adding ValueEventListener to child node: keyChildNode051 
Adding ValueEventListener to child node: keyChildNode052 
Adding ValueEventListener to child node: keyChildNode053 
Adding ValueEventListener to child node: keyChildNode054 
Adding ValueEventListener to child node: keyChildNode055 
Adding ValueEventListener to child node: keyChildNode056 
Adding ValueEventListener to child node: keyChildNode057 
Adding ValueEventListener to child node: keyChildNode058 
Adding ValueEventListener to child node: keyChildNode059 
Adding ValueEventListener to child node: keyChildNode060 
Adding ValueEventListener to child node: keyChildNode061 
Adding ValueEventListener to child node: keyChildNode062 
Adding ValueEventListener to child node: keyChildNode063 
Adding ValueEventListener to child node: keyChildNode064 
Adding ValueEventListener to child node: keyChildNode065 
Adding ValueEventListener to child node: keyChildNode066 
Adding ValueEventListener to child node: keyChildNode067 
Adding ValueEventListener to child node: keyChildNode068 
Adding ValueEventListener to child node: keyChildNode069 
Adding ValueEventListener to child node: keyChildNode070 
Adding ValueEventListener to child node: keyChildNode071 
Adding ValueEventListener to child node: keyChildNode072 
Adding ValueEventListener to child node: keyChildNode073 
Adding ValueEventListener to child node: keyChildNode074 
Adding ValueEventListener to child node: keyChildNode075 
Adding ValueEventListener to child node: keyChildNode076 
Adding ValueEventListener to child node: keyChildNode077 
Adding ValueEventListener to child node: keyChildNode078 
Adding ValueEventListener to child node: keyChildNode079 
Adding ValueEventListener to child node: keyChildNode080 
Adding ValueEventListener to child node: keyChildNode081 
Adding ValueEventListener to child node: keyChildNode082 
Adding ValueEventListener to child node: keyChildNode083 
Adding ValueEventListener to child node: keyChildNode084 
Adding ValueEventListener to child node: keyChildNode085 
Adding ValueEventListener to child node: keyChildNode086 
Adding ValueEventListener to child node: keyChildNode087 
Adding ValueEventListener to child node: keyChildNode088 
Adding ValueEventListener to child node: keyChildNode089 
Adding ValueEventListener to child node: keyChildNode090 
Adding ValueEventListener to child node: keyChildNode091 
Adding ValueEventListener to child node: keyChildNode092 
Adding ValueEventListener to child node: keyChildNode093 
Adding ValueEventListener to child node: keyChildNode094 
Adding ValueEventListener to child node: keyChildNode095 
Adding ValueEventListener to child node: keyChildNode096 
Adding ValueEventListener to child node: keyChildNode097 
Adding ValueEventListener to child node: keyChildNode098 
Adding ValueEventListener to child node: keyChildNode099 
Adding ValueEventListener to child node: keyChildNode100 
Datasnapshot retrieved for child node: keyChildNode001 
Datasnapshot retrieved for child node: keyChildNode002 
Datasnapshot retrieved for child node: keyChildNode003 
Datasnapshot retrieved for child node: keyChildNode004 
Datasnapshot retrieved for child node: keyChildNode005 
Datasnapshot retrieved for child node: keyChildNode006 
Datasnapshot retrieved for child node: keyChildNode007 
Datasnapshot retrieved for child node: keyChildNode008 
Datasnapshot retrieved for child node: keyChildNode009 
Datasnapshot retrieved for child node: keyChildNode010 
Datasnapshot retrieved for child node: keyChildNode011 
Datasnapshot retrieved for child node: keyChildNode012 
Datasnapshot retrieved for child node: keyChildNode013 
Datasnapshot retrieved for child node: keyChildNode014 
Datasnapshot retrieved for child node: keyChildNode015 
Datasnapshot retrieved for child node: keyChildNode016 
Datasnapshot retrieved for child node: keyChildNode017 
Datasnapshot retrieved for child node: keyChildNode018 
Datasnapshot retrieved for child node: keyChildNode019 
Datasnapshot retrieved for child node: keyChildNode020 
Datasnapshot retrieved for child node: keyChildNode021 
Datasnapshot retrieved for child node: keyChildNode022 
Datasnapshot retrieved for child node: keyChildNode023 
Datasnapshot retrieved for child node: keyChildNode024 
Datasnapshot retrieved for child node: keyChildNode025 
Datasnapshot retrieved for child node: keyChildNode026 
Datasnapshot retrieved for child node: keyChildNode027 
Datasnapshot retrieved for child node: keyChildNode028 
Datasnapshot retrieved for child node: keyChildNode029 
Datasnapshot retrieved for child node: keyChildNode030 
Datasnapshot retrieved for child node: keyChildNode031 
Datasnapshot retrieved for child node: keyChildNode032 
Datasnapshot retrieved for child node: keyChildNode033 
Datasnapshot retrieved for child node: keyChildNode034 
Datasnapshot retrieved for child node: keyChildNode035 
Datasnapshot retrieved for child node: keyChildNode036 
Datasnapshot retrieved for child node: keyChildNode037 
Datasnapshot retrieved for child node: keyChildNode038 
Datasnapshot retrieved for child node: keyChildNode039 
Datasnapshot retrieved for child node: keyChildNode040 
Datasnapshot retrieved for child node: keyChildNode041 
Datasnapshot retrieved for child node: keyChildNode042 
Datasnapshot retrieved for child node: keyChildNode043 
Datasnapshot retrieved for child node: keyChildNode044 
Datasnapshot retrieved for child node: keyChildNode045 
Datasnapshot retrieved for child node: keyChildNode046 
Datasnapshot retrieved for child node: keyChildNode047 
Datasnapshot retrieved for child node: keyChildNode048 
Datasnapshot retrieved for child node: keyChildNode049 
Datasnapshot retrieved for child node: keyChildNode050 
Datasnapshot retrieved for child node: keyChildNode051 
Datasnapshot retrieved for child node: keyChildNode052 
Datasnapshot retrieved for child node: keyChildNode053 
Datasnapshot retrieved for child node: keyChildNode054 
Datasnapshot retrieved for child node: keyChildNode055 
Datasnapshot retrieved for child node: keyChildNode056 
Datasnapshot retrieved for child node: keyChildNode057 
Datasnapshot retrieved for child node: keyChildNode058 
Datasnapshot retrieved for child node: keyChildNode059 
Datasnapshot retrieved for child node: keyChildNode060 
Datasnapshot retrieved for child node: keyChildNode061 
Datasnapshot retrieved for child node: keyChildNode062 
Datasnapshot retrieved for child node: keyChildNode063 
Datasnapshot retrieved for child node: keyChildNode064 
Datasnapshot retrieved for child node: keyChildNode065 
Datasnapshot retrieved for child node: keyChildNode066 
Datasnapshot retrieved for child node: keyChildNode067 
Datasnapshot retrieved for child node: keyChildNode068 
Datasnapshot retrieved for child node: keyChildNode069 
Datasnapshot retrieved for child node: keyChildNode070 
Datasnapshot retrieved for child node: keyChildNode071 
Datasnapshot retrieved for child node: keyChildNode072 
Datasnapshot retrieved for child node: keyChildNode073 
Datasnapshot retrieved for child node: keyChildNode074 
Datasnapshot retrieved for child node: keyChildNode075 
Datasnapshot retrieved for child node: keyChildNode076 
Datasnapshot retrieved for child node: keyChildNode077 
Datasnapshot retrieved for child node: keyChildNode078 
Datasnapshot retrieved for child node: keyChildNode079 
Datasnapshot retrieved for child node: keyChildNode080 
Datasnapshot retrieved for child node: keyChildNode081 
Datasnapshot retrieved for child node: keyChildNode082 
Datasnapshot retrieved for child node: keyChildNode083 
Datasnapshot retrieved for child node: keyChildNode084 
Datasnapshot retrieved for child node: keyChildNode085 
Datasnapshot retrieved for child node: keyChildNode086 
Datasnapshot retrieved for child node: keyChildNode087 
Datasnapshot retrieved for child node: keyChildNode088 
Datasnapshot retrieved for child node: keyChildNode089 
Datasnapshot retrieved for child node: keyChildNode090 
Datasnapshot retrieved for child node: keyChildNode091 
Datasnapshot retrieved for child node: keyChildNode092 
Datasnapshot retrieved for child node: keyChildNode093 
Datasnapshot retrieved for child node: keyChildNode094 
Datasnapshot retrieved for child node: keyChildNode095 
Datasnapshot retrieved for child node: keyChildNode096 
Datasnapshot retrieved for child node: keyChildNode097 
Datasnapshot retrieved for child node: keyChildNode098 
Datasnapshot retrieved for child node: keyChildNode099 
Datasnapshot retrieved for child node: keyChildNode100 

一つのことは私を驚か:子の参照に追加ValueEventListenersは本質的に非同期であるため、私はスナップショットはランダムで取得することが期待(必ずしもValueEventListenersが追加されたのと同じ順序である必要はありません)。しかしコンソール出力は、ValueEventListenersが各子ノードに追加された順序でスナップショットが取得されることを示しています。

この「整然とした」方法でスナップショットが取得されたのは正常ですか?はいの場合、リスナーの非同期性を考えればどのように可能ですか?前もって感謝します。

答えて

4

これは実際に予想される動作です。

Firebaseデータベースクライアントは、単一のソケット接続を介してサーバと通信します。このソケットは、クライアントとデータベースサーバー間のすべての通信に使用されます。

100件のレコードをリクエストしている場合は、すべての要求が、この1回の接続で、呼び出した順に送信されます。次に、サーバーは同じ順序で要求を実行し、結果を同じ順序で返します。

これは、パイプライン処理と呼ばれます.1つの接続で複数の要求を送信し、結果を順番に返すことです。私はここまでにもう少し答えてきました。Speed up fetching posts for my social network app by using query instead of observing a single event repeatedly

+0

すぐに答えてくれてありがとうございました!それは、私に、その主題をさらに研究し、パイプラインについてもっと学ぶための確かな土台を与えました。良い仕事を続けてください。 –

関連する問題