0

following is my schema where one user has assigned specific orders and one order could have multiple items

Master JSON
-----------
users
    user-id
        email
        username

orders
    order-id
        orderNo
        orderStatus
        deliveryDate

items
    item-id
        itemNo
        itemName
        itemQty
        itemPrice

Related JSON    
------------
    user-orders
        user-id
            order-id                
                orderNo
                orderStatus
                deliveryDate

    order-itmes
        order-id
            item-id
                itemNo
                itemName
                itemQty
                itemPrice

i am accessing all orders belong to one user as follows

dbRef.child("user-orders").child(userId);

and updating listview as follows inside ChildEventListener

 public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Order order = dataSnapshot.getValue(Order.class);
            listview.add(order);
            adapter.notifyParentItemInserted(listview.size()-1);

        }

but at the same time i want to fetch all items belong to all orders as well , hence i need to use following query inside onChildAdded and will be needing another ChildEventListener listener.

dbRef.child("order-items").child(dataSnapshot.getKey());/ /key of newly fetched order.

so question is , firebase has any way to fetch all items belong to all orders inside one listener ? like if i pass all order keys in one go to fetch all items belong to those orders.

i am displaying order and items details on one list only hence i need both data in one go

EDIT

calling orders

 public void readOrders(String userId){

        mOrderQuery = mDatabaseRef.child("user-orders").child(userId);
        mOrderQuery.addChildEventListener(mOrderEventListener);

    }

Listener to read orders

private ChildEventListener mOrderEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Order order = dataSnapshot.getValue(Order.class);
            orderList.add(order);
            // fetching items belong to specific order 
            mItemQuery = mDatabaseRef.child("order-items").child(dataSnapshot.getKey());

            // problem is here for every order i have to register a event listener and this not the right way to do it 
            mItemQuery.addChildEventListener(mItemEventListener);


        }

        public void onChildChanged(DataSnapshot dataSnapshot, String s) { }
        public void onChildRemoved(DataSnapshot dataSnapshot) { }
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
        public void onCancelled(DatabaseError databaseError) {
            Log.e(TAG , "onCancelled called");
        }
    };

Listener to read items

private ChildEventListener mItemEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            Item item = dataSnapshot.getValue(Item.class);
           // here i need to map the incoming item to specific order list


        }

        public void onChildRemoved(DataSnapshot dataSnapshot) { }
        public void onChildMoved(DataSnapshot dataSnapshot, String s) { }
        public void onCancelled(DatabaseError databaseError) { }
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            Log.e(TAG , "onChildChanged called");
        }
    };
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Hunt
  • 8,215
  • 28
  • 116
  • 256

1 Answers1

0

There is no Firebase equivalent to SQL's WHERE id IN (1,2,3).

In Firebase you have to retrieve each item individually. But this is not as slow as you may think, since Firebase pipelines the requests.

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • okay so how i will be needing separate listener to fetch items and then i manually add it under specific order , wouldn't that be cumbersome process ? – Hunt Oct 01 '16 at 15:21
  • That's subjective, so I'm not sure what you're looking for. If you write the code and it doesn't work, post the [minimum code that reproduces the problem](http://stackoverflow.com/help/mcve). If you have code and wonder if it could be simplified, you might want to post that either here or on codereview.stackexchange.com. – Frank van Puffelen Oct 01 '16 at 15:32