Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cloud Firestore Data Structure making effectively

I’m thinking of RecyclerView.

For example, users can view posts by region. So I thought of calling the post object differently depending on the region like post1,post2,post3, … <- collection. A different post object is called for each region set by the user.

I wonder if it’s okay to think like this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It makes sense to create a collection that looks like this:

Firestore-root
  |
  --- posts (collection)
       |
       --- $postId
       |     |
       |     --- region: "USA"
       |     |
       |     --- title: "Post one title"
       |
       --- $postId
             |
             --- region: "Europe"
             |
             --- title: "Post teo title"

And if you want to filter the posts by a specific region, then you should simply perform a query that looks like this:

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference postsRef = db.collection("posts");
Query queryByRegion = postsRef.whereEqualTo("region", "Europe");
queryByRegion.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                if (document != null) {
                    String title = document.getString("title");
                    Log.d(TAG, title);
                }
            }
        } else {
            Log.d(TAG, task.getException().getMessage());
        }
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading