2016-08-25 8 views
0

Firebaseにエントリを格納するアプリケーションを作成しようとしています。各エントリには、誰が書いたかを指定する著者フィールドがあります。しかし、すべての投稿(著者に関係なく)は共通の親にプッシュされます。この親の下の各エントリを繰り返し処理し、与えられた著者が投稿をすべて見つける方法はありますか?JavaのFirebase Search by Child Value

+0

https://firebase.google.com/docs/database/android/retrieve-data#sorting_and_filtering_dataを参照してください。しかし、https://firebase.google.com/docs/database/android/structure-data#fanoutで説明されているように、最適なスケーラビリティのためにデータを非正規化することも検討してください –

答えて

0

FirebaseQueryを使用できます。基本的には、すべてのデータベースをダウンロードして自分でフィルタリングする代わりに、特定の子供の値でFirebaseReferenceを照会します。このようなことを試して、同じ著者からのすべての投稿を取得することができます。

// Get your reference to the node with all the entries 
DatabaseRefenrec ref = FirebaseDatabase.getInstance().getReference(); 

// Query for all entries with a certain child with value equal to something 
Query allPostFromAuthor = ref.orderByChild("name_of_the_child_node").equalTo("author_name"); 

// Add listener for Firebase response on said query 
allPostFromAuthor.addValueEventListener(new ValueEventListener(){ 
    @Override 
    public void onDataChange(DataSnapshot dataSnapshot) { 
     for(DataSnapshot post : dataSnapshot.getChildren()){ 
      // Iterate through all posts with the same author 
     } 
    } 

    @Override 
    public void onCancelled(DatabaseError databaseError) {} 
}); 

より良いパフォーマンスのために、Firebaseルールを使用してindexingあなたのデータベースを検討してください。これにより、Firebaseはデータを順序付けられた方法で保存するので、クエリがより速く管理されます。