1

How do I query SQL IN clause in Firebase Android? I want to use it in a Firebase Recycler adapter to retrieve only some children based on some condition. Something like the following statement:

SQL----> select * from posts where city in(10 cities comes here)

I need a Firebase query to use that in the Firebase Recycler adapter.

Daniel
  • 2,355
  • 9
  • 23
  • 30
Vignesh VRT
  • 399
  • 3
  • 15

2 Answers2

2

The Firebase database does not have the equivalent of SQL's WHERE id IN (1,2,3). In the case of selecting by ID, Firebase's way of retrieving items is equally fast because Firebase pipelines the requests.

Your case is different though, since you're not selecting by ID. Unfortunately there is no way to directly map your query to a equivalent on the Firebase Database.

Instead of trying to make Firebase's NoSQL database do SQL tricks, I highly recommend that you start mapping your data model to something that fits better with a NoSQL database. Some great resources to kick start this process are this article on NoSQL data modeling and our new video series on Firebase for SQL developers.

Also see Firebase complex "contain" queries

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • what have to do when we are selecting using ID? let suppose we have totally 10 posts .. and we want to retrieve only some users posts.....for example in facebook we get only our friends posts.....like that we should we do to achive my requirements ... please help me out sir..... now im doing my college project using firebase....it is a app like facebook... sending requests to friends ... seeing their posts on timeline and so on – Vignesh VRT Dec 02 '16 at 11:51
  • So your solution to [the problem where an user vote has to be displayed](https://stackoverflow.com/questions/45865924/firebase-getting-around-joins-example-display-star-count-for-each-user) is to just run every request for each item vote? I know the requests are run in parallel and I can save a round trip by doing it all in a cloud function but that still sounds hella wasteful. – Ced Sep 03 '17 at 21:09
1

I found the solution: we cannot use FirebaseRecyclerAdapter. Instead we have to create custom adapter that extends RecyclerView.ViewHolder.

For passing values to this adapter, first we have to retrieve data using addValueEventListener and then we have to pass values to our adapter.

This is my code...

final ArrayList<Timeline> timelines = new ArrayList<>();

    mDatabaseTimeline.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            final Timeline timeline = dataSnapshot.getValue(Timeline.class);

            if(timeline != null){

                mDatabaseFriends.child(mAuth.getCurrentUser().getUid()).child("active").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {

                        if (mAuth.getCurrentUser().getUid().equals(timeline.getUid()) || dataSnapshot.hasChild(timeline.getUid())) {

                            timelines.add(timeline);
                            mTimelineRecycler.setAdapter(new RecyclerAdapter(TimelineFragment.this.getContext(), timelines));

                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

adapter----->

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    ArrayList<Timeline> timeline;


    public RecyclerAdapter(Context context, ArrayList<Timeline> timeline) {
        this.context = context;
        this.timeline = timeline;



    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View row = inflater.inflate(R.layout.timeline_row, parent, false);
        TimelineViewHolder holder = new TimelineViewHolder(row);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        final String post_key = timeline.get(position).getPostkey();

        ((TimelineViewHolder) holder).setUsername(timeline.get(position).getUsername());


 }

    @Override
    public int getItemCount() {
        return timeline.size();
    }

    public class TimelineViewHolder extends RecyclerView.ViewHolder {
        public TimelineViewHolder(View itemView) {
            super(itemView);

            view = itemView;
        }

        public View getView() {
            return view;
        }    

        public void setUsername(String username) {
            TextView usernameTxtView = (TextView) view.findViewById(R.id.timeline_username);
            usernameTxtView.setText(username);
        }

    }
}
Vignesh VRT
  • 399
  • 3
  • 15