Fetch SubCollection data into Recyclerview - android

I'm trying to retrieve data from a Firebase sub collection in a recyclerview. I think it must be possible but what I did was not right I think
I want to recover the data from REPARS_TABLE in Recyclerview
Android - Cloud Firestore
Query query = db.collection("RESTO_TABLE").document().collection("REPARS_TABLE").orderBy("nomRepars");
FirestoreRecyclerOptions<ReparsModele> OptionRepars = new FirestoreRecyclerOptions.Builder<ReparsModele>()
.setQuery(query,ReparsModele.class)
.build();
Log.d("SHOW","SHOW QUERY"+ query);
bureauReparsAdapter = new BureauReparsAdapter(OptionRepars);
repaLists.setHasFixedSize(true);
repaLists.setLayoutManager(new LinearLayoutManager(getContext()));
repaLists.setAdapter(bureauReparsAdapter);
bureauReparsAdapter.notifyDataSetChanged();

You will need to call out the ID of the document whose subcollection you want to you. Right now, you're calling document() with no parameters, which means it's generating a random ID as part of the path. You will need to instead pass it the ID of the document.
If you don't know the ID ahead of time, then you won't be able to do this query, or you will have to use a collection group query and filter the documents you want from all of the subcollections named "REPARS_TABLE".

Related

Not able to use getRef() of Firebase in android

I need to update the data in Firebase database. I have displayed the data in Recyclerview. I need to get the child reference of the position i click in Recyclerview. An not able to use getRef() there to get the reference.
You need to generate a unique key with
String unique = reference.push().getKey();
when saving data to firebase, then you can use that unique key to modify content of that node later.
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Transactions").child(uniqueId)...
You can show your getRef(position) method in your question, maybe another solution can come from there.

Is it possible to use Where In with a Collection Group query in Firestore?

In my database:
-Users(Top level collection):
---user1(doc):
----------pets(sub collection):
--------------pet1(doc)
--------------pet2(doc)
---user2(doc):
----------pets(sub collection):
--------------pet3(doc)
--------------pet4(doc)
If i have a List of ids: list("pet2", "pet3", "pet4)
Is there a way to do something like this and get a back a List of DocumentsSnapshots?
firestore.collectionGruop("pets")whereIn(FieldPath.documentId(), list)
It works with a root collection but i dont know if this is possible with a collection group
A collection group query is the only place where a filter on FieldPath.documentId() does not work the way you expect. That's because of some details about the way that this token actually works. If you try to this anyway, you will get an error like this:
Invalid query. When querying a collection group by FieldPath.documentId(), the value provided must result in a valid document path, but 'x' is not because it has an odd number of segments.
If you want to do a filter on document IDs in a collection group query, you will need to store the ID of the document as the value of a field in each document. If you use the field called "id", then you can filter on that field like this:
firestore
.collectionGruop("pets")
.whereIn('id', list)
This will give you a different error saying that you need to create an index, and give you a link to do so. After you create that index (it might take some time), you should be good to go.
See also: How to perform collection group query using document ID in Cloud Firestore

Is there any way to use OR query with Firestore RecyclerAdapter?

I have a recyclerview using Firestore Recyclerview Adapter, Now I want to show the list by using OR query.
For example I want to show the list which are either in "Pending" state OR "Assigned" state using query. I know firestore doesn't have OR query inbuilt but I want to achieve this anyhow. Any alrernative solution.
I have stored 4 state in firestore db : Pending, Assigned, Accepted and Completed.
Only want to show pending or assigned
Below is my code:
Query query = requestVehiclesCollectionReference
.whereEqualTo("clientId", id)
// show list which are in Pending state or Assigned state
.whereEqualTo("status", "Pending")
.orderBy("createdAt", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Item> options = new
FirestoreRecyclerOptions.Builder<Item>()
.setQuery(query, Item.class)
.build();
Firestore doesn't support queries with OR conditions. See:
Implementing OR in firestore query - Firebase firestore
How to perform compound queries with logical OR in Cloud Firestore?
Firebase Firestore - OR query
The common workaround is to use multiple queries (one for each condition) and merge the results in your application code.
FirebaseUI's adapters show data from a single query or collection. They have no option to show the results from multiple queries/collections.
From this combination it follows that you can't show data from an OR condition with a single FirebaseUI adapter. You will have to create you own adapter for this, although you can certainly use the (open-source) FirebaseUI adapters for inspiration.

Android's Firestore to join 2 collections into a recycler view

I have a users collection with uId, name, photo
I have a visits collection with uId, userId, location
I have a recyclerview in which I want to show the location with the user name and photo
Can I use the reference field type? If so, how will Firestore know to link visits.userId == users.uId ?
Maybe I first need to query all the visits and then query the relevant user but 2 things:
It means querying a lot of times.
I didn't understand how to collect the joined collection into the adapter, which is based on one query?
Please advice
Thanks
current code
visitsList = db.collection("visitsList");
Query query = visitsList.whereEqualTo("userId",prefs.getString("id","")).orderBy("visitDate", Query.Direction.ASCENDING);
FirestoreRecyclerOptions<AVisit> options = new FirestoreRecyclerOptions.Builder<AVisit>().setQuery(query, AVisit.class).build();
adapter = new VisitsListAdapter(options, VisitsListActivity.this);
RecyclerView rv = findViewById(R.id.rvVisitsList);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(adapter);
The code is a simple query from the collection, not sure how to get the name and photo from the userId field in that collection.
Can I use the reference field type?
Yes, you can use a reference field.
If so, how will Firestore know to link visits.userId == users.uId ?
Firestore results always comes from a single collection (at the moment). It does not automatically join the document from the users collection when you're reading from the visits collection. You will have to do this yourself.
That indeed means you'll be executing multiple reads, but it's often not nearly as slow as you may think. See Google Firestore - how to get document by multiple ids in one round trip?
Update: To show data from the user profile in a list of visits, there are two main options:
load the additional user document in populateView or with a custom parseSnapshot implementation.
duplicate the relevant user data in the visits collection (which is quite normal in NoSQL databases). Also see Alex' answer here: indexed query with FirestoreRecyclerAdapter.

How to save data on top of the already saved data in Firebase?

I am saving some data in FirebaseDatabase under a child as a key:value pair.
The problem is that data is getting saved as pushed, like if data1 is saved already than data2 will get saved below it. I want to save it above this already saved data.
Here is the data structure:
- branch
- child
- uniqueKey1: data1
- uniqueKey2: data2
I am saving the data using this code:
String key = mDatabase.child("branch").child("child").push().getKey();
mDatabase.child("branch").child("child").child(key).setValue(data);
What I want is the structure below:
- branch
- child
- uniqueKey2: data2
- uniqueKey1: data1
How to save the newly added data above the already saved data? Please let me know.
In your firebase database, childs in a node are ordered in alphabetical order, no matter which order you save them.
If you want to retrieve them later in some specific order, let's say order by date added, you might want to create a timestamp reference.
Check here:
How to sort value inside a Firebase child to the rest?
Edit: a lot of answers and edited questions while writing my answer, but as others mentioned you should not worry about the order you see the data in the database, you should only care to provide the right structure to retrieve the data correctly.
As I said, in the DB the childs are ordered in alphabetical order so if you insist on ordering it by date added you should figure out a way to update the key accordingly and then update the whole node.
I don't think the order in which you save data is important as long as you have a strategy to retrieve it correctly in your order of choice. Firebase provides API to retrieve data ordered by either key or value.
Work with Lists of Data on Android
If you just want to retrieve the record first which was added last, you can put a timestamp that accompanies your data. There are also methods that gets you last record of a collection.
What you are trying to do is update data1. Then what you have to do is,
Get reference of the node you want to update.
DatabaseReference ref = mDatabase.child("branch").child("child").child("data1");
then update the child node. I'm assuming there is a child name under data1, whose value is 'Foo'. And now you want to update it 'Bar'.
Hashmap<String, String> map = new Hashmap();
map.put("name","Bar");
ref.updateChildren(map);
This is how you update a node. If you want to completely replace the node, then you can delete the node and by calling removeValue() method and pushing the data again!.

Categories

Resources