Bucky
Bucky

Reputation: 1206

Getting posts from people I follow using Firestore

I'm using Cloud Firestore on my mobile app. I want to get contents from users I follow. Right now those:

enter image description here

So I'm trying the following query to get contents from people I follow.

ref = FirebaseFirestore.getInstance().collection("topics");
ref.whereEqualTo("username","user1");
ref.whereEqualTo("username","user2");

But when I use this query it only shows posts from first user (user1).

Also what If I follow 10K users. What's the best way to get latest posts from them?

Upvotes: 1

Views: 2142

Answers (3)

Nader Khaled
Nader Khaled

Reputation: 155

I have recently found a solution for this type of problem. You can check it out here at this link : https://stackoverflow.com/a/70861741/11351916

Upvotes: 0

Odai A. Ali
Odai A. Ali

Reputation: 1225

There are two ways to deal with this thing. The first is by using cloud functions for Firebase to listen for any document created by the user and then calls a method which will send a copy of the document created to every user who is following. The second is that one Doug Stevenson mentioned, basically you have to create a collection or a document containing all the users you follow then from that you can call for each one to get your data.

Upvotes: 2

Doug Stevenson
Doug Stevenson

Reputation: 317710

That kind of query you're trying to perform isn't supported by Firestore. You're assuming that adding more calls to whereEqualTo() will each apply like a logical OR, and that any document that matches any of the conditions will be returned. In Firestore, adding more conditions is a logical AND, meaning all of the conditions must be true for a document to match.

You will have to restructure your database in order to support the type of query you want to perform. This is very common for noSQL type databases. There is not a single structure that supports all kinds of queries. In your case, you will need to record per-user which other users they follow, and query that to get what you want. You may need to perform multiple queries to get everything required for your case.

Upvotes: 4

Related Questions