2017-02-23 141 views
0

我目前正在使用Firebase編寫Android應用程序。我有一個具有城市屬性的產品模型。 我運行下面的查詢:Android Firebase orderByChild查詢

mDatabaseReference = FirebaseDatabase.getInstance().getReference(); 
mDatabaseReference.keepSynced(true); 

productQuery = mDatabaseReference.child("products") 
        .orderByChild("city").equalTo(mCurrentUser).limitToFirst(100); 

這個查詢檢索所有數據庫中的產品,無論城市。

我花了最近四個小時在SO上查看類似的問題,嘗試查詢函數+規則索引的不同組合,無濟於事。 我有什麼我稱之爲愚蠢的解決方案,重新複製城市產品樹下的數據。不過,我猜測有一個更好的方法來利用orderByChild,如果我能得到一些幫助按城市查詢數據,我將不勝感激。

感謝您的輸入

這裏的CityFeed片斷 - 注意:我查mCurrentUserCity設置

public class CityFeedFragment extends BaseFragment { 
    private RecyclerView mRecyclerView; 
    private String mCurrentUserCity; 
    private FirebaseProductAdapter mAdapter; 
    private DatabaseReference mDatabaseReference; 
    private LinearLayoutManager mManager; 
    private Query productQuery; 
    private SwipeRefreshLayout mSwipeRefreshLayout; 

    @Override 
    public void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_feed, container, false); 
     mDatabaseReference = FirebaseDatabase.getInstance().getReference(); 
     mDatabaseReference.keepSynced(true); 

     retrieveCity(); 
     fetchProducts(); 

     mAdapter = new FirebaseProductAdapter(Product.class, 
       R.layout.feed_item, ProductViewHolder.class, 
       productQuery.getRef(), getContext(), false); 

     return view; 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 

     mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); 
     mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout); 
     mRecyclerView.setAdapter(mAdapter); 

     mManager = new LinearLayoutManager(getActivity()); 
     mManager.setReverseLayout(true); 
     mManager.setStackFromEnd(true); 

     mRecyclerView.setHasFixedSize(true); 

     mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
     mRecyclerView.setLayoutManager(mManager); 


     mSwipeRefreshLayout.setOnRefreshListener(() -> { 
      fetchProducts(); 
      mAdapter.notifyDataSetChanged(); 
      mSwipeRefreshLayout.setRefreshing(false); 
     }); 

    } 


    private void fetchProducts() { 
     productQuery = mDatabaseReference.child("products").orderByChild("city").equalTo(mCurrentUserCity); 
    } 

    private void retrieveCity() { 
     mDatabaseReference.child("users").child(getUid()).addListenerForSingleValueEvent(
       new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         User user = dataSnapshot.getValue(User.class); 
         if (user == null) { 
          showToast(getResources().getString(R.string.empty_user)); 
         } else { 
          mCurrentUserCity = user.getCity(); 
         } 
        } 
        @Override 
        public void onCancelled(DatabaseError databaseError) { 
         showToast(databaseError.toString()); 
         Log.w("Cityfeed", "getUser:onCancelled", databaseError.toException()); 
        } 
       } 
     ); 
    } 

} 

數據結構 enter image description here

+1

請問您可以發佈您的數據庫結構的圖片,我並不真正瞭解您的問題是什麼。 –

+0

另請參見:請包含在讀取數據時再現問題的最小代碼。您現在共享的代碼僅創建一個查詢,但尚未檢索任何數據。如果你有一個監聽器只能記錄結果,那麼沒關係,但我仍然希望看到它。 –

+0

完成,謝謝觀看 – Ender

回答

2

你所做的一切,只是加入的偵聽器獲取值作爲數據快照。請用您的方法替換您的方法 -

private void fetchProducts() { 
    productQuery = mDatabaseReference.child("products").orderByChild("city").equalTo(mCurrentUserCity).addListenerForSingleValueEvent(
      new ValueEventListener() { 
       @Override 
       public void onDataChange(DataSnapshot dataSnapshot) { 
        //Map of all products which has city equal to mCurrentUserCity 
       } 
       @Override 
       public void onCancelled(DatabaseError databaseError) { 

       } 
      } 
    ); 
}