FirebaseRecyclerView - make onBindViewHolder method create severals elements - java

I am creating a FirebaseRecyclerView.
I give to my FirebaseRecyclerView a Query that refers to objects from my FirestoreDatabase.
But instead of having 1 element for each object, I want to create severals elements.
Example of 1 object structure :
Firestore-root
|
--- collection (collection)
|
--- document1 (document)
| |
| --- documentField (List<Object>): "Data1"
| : "Data2"
| : "Data3"
What I can do :
View_I_Can_Do
What I want to do :
View_I_Want_To_Have
NB : The number of ''Data'' is changing, so I can't design an itemView to match with an undefined number of variables.
I suppose the solution is in the onBindViewHolder method, but I don't know how to make it works.
Don't hesitate to ask for more details if needed.
Any help or suggestion would be appreciated, thanks in advance !
Edit 01/21/2022 :
I'm still stuck on this ...
I tried to give more detail in this question : Display one item for each object in a single Firebase document field - FirebaseRecyclerView
Maybe this is more explicit.

Related

How to create category containing products in firestore firebase

I am using Cloud Firestore to make my own E-commerce App. I want my category collection to contain many categories like shoes, clothes, watches, etc and in each of those categories there are different products so I can click on that category and show all the related products on my app, please guide me or if you have another way to do this, please show me.
Category and Products
According to your comment:
just tens, this is just my personal project.
Since you only have tens of category names, then the best option that you have is to store them in a single document, in an array type field. Then each product document should contain a field where you have to specify the category. Your schema should look like this:
Firestore-root
|
--- shop (collection)
| |
| --- category (document)
| |
| --- names: ["shoes", "clothes", "watches"]
|
--- products (collection)
|
--- $productId (document)
|
--- name: "Nike Air Jordan"
|
--- category: "shoes"
To be able to display the category names, you should simply read the array that exists in the document, and display the content into a ListView, or even better, in a RecyclerView.
Furthermore, if you need to click on a particular category, and go forward to display only the products that correspond to a specific category, then you should use the following query:
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query queryByCategory = db.collection("products").whereEqualTo("category", "shoes");
For displaying the products you might consider reading my answer from the following post:
How to display data from Firestore in a RecyclerView with Android?
The following article might also help you read the data with the use of the Firebase-UI library:
How to read data from Cloud Firestore using get()?

REST API for updating informations with empty or null values

I have a general question about how best to build an API that can modify records in a database.
Suppose we have a table with 10 columns and we can query these 10 columns using REST (GET). The JSON response will contain all 10 fields. This is easy and works without problems.
The next step is that someone wants to create a new record via POST. In this case the person sends only 8 of the 10 fields in the JSON Request. We would then only fill the 8 fields in the database (the rest would be NULL). This also works without problems.
But what happens if someone wants to update a record? We see here different possibilities with advantages and disadvantages.
Only what should be updated is sent.
Problem: How can you explicitly empty / delete a field? If a "NULL" is passed in the JSON, we get NULL in the object, but any other field that is not passed is NULL as well. Therefore we cannot distinguish which field can be deleted and which field cannot be touched.
The complete object is sent.
Problem: Here the object could be fetched via a GET before, changed accordingly and returned via PUT. Now we get all information back and could write the information directly back into the database. Because empty fields were either already empty before or were cleared by the user.
What happens if the objects are extended by an update of the API. Suppose we extend the database by five more fields. The user of the API makes a GET, gets the 15 fields, but can only read the 10 fields he knows on his page (because he hasn't updated his side yet). Then he changes some of the 10 fields and sends them back via PUT. We would then update only the 10 fields on our site and the 5 new fields would be emptied from the database.
Or do you have to create a separate endpoint for each field? We have also thought about creating a map with key / value, what exactly should be changed.
About the technique: We use the Wildfly 15 with Resteasy and Jackson.
For example:
Database at the beginning
+----+----------+---------------+-----+--------+-------+
| ID | Name | Country | Age | Weight | Phone |
+----+----------+---------------+-----+--------+-------+
| 1 | Person 1 | Germany | 22 | 60 | 12345 |
| 2 | Person 2 | United States | 32 | 78 | 56789 |
| 3 | Person 3 | Canada | 52 | 102 | 99999 |
+----+----------+---------------+-----+--------+-------+
GET .../person/2
{
"id" : 2,
"name" : "Person 2",
"country" : "United States",
"age" : 22,
"weight" :62,
"phone": "56789"
}
Now I want to update his weight and remove the phone number
PUT .../person/2
{
"id" : 2,
"name" : "Person 2",
"country" : "United States",
"age" : 22,
"weight" :78
}
or
{
"id" : 2,
"name" : "Person 2",
"country" : "United States",
"age" : 22,
"weight" :78,
"phone" : null
}
Now the database should look like this:
+----+----------+---------------+-----+--------+-------+
| ID | Name | Country | Age | Weight | Phone |
+----+----------+---------------+-----+--------+-------+
| 1 | Person 1 | Germany | 22 | 60 | 12345 |
| 2 | Person 2 | United States | 32 | 78 | NULL |
| 3 | Person 3 | Canada | 52 | 102 | 99999 |
+----+----------+---------------+-----+--------+-------+
The problem is
We extend the table like this (salery)
+----+----------+---------------+-----+--------+--------+-------+
| ID | Name | Country | Age | Weight | Salery | Phone |
+----+----------+---------------+-----+--------+--------+-------+
| 1 | Person 1 | Germany | 22 | 60 | 1929 | 12345 |
| 2 | Person 2 | United States | 32 | 78 | 2831 | NULL |
| 3 | Person 3 | Canada | 52 | 102 | 3921 | 99999 |
+----+----------+---------------+-----+--------+--------+-------+
The person using the API does not know that there is a new field in JSON for the salary. And this person now wants to change the phone number of someone again, but does not send the salary. This would also empty the salary:
{
"id" : 3,
"name" : "Person 3",
"country" : "Cananda",
"age" : 52,
"weight" :102,
"phone" : null
}
+----+----------+---------------+-----+--------+--------+-------+
| ID | Name | Country | Age | Weight | Salery | Phone |
+----+----------+---------------+-----+--------+--------+-------+
| 1 | Person 1 | Germany | 22 | 60 | 1929 | 12345 |
| 2 | Person 2 | United States | 32 | 78 | 2831 | NULL |
| 3 | Person 3 | Canada | 52 | 102 | NULL | NULL |
+----+----------+---------------+-----+--------+--------+-------+
And salary should not be null, because it was not set inside the JSON request
You could deserialize your JSON to a Map.
This way, if a property has not been sent, the property is not present in the Map. If its null, its inside the map will a null value.
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String, Object>> typeReference = new TypeReference<>() {};
HashMap<String, Object> jsonMap = mapper.readValue(json, typeReference);
jsonMap.entrySet().stream().map(Map.Entry::getKey).forEach(System.out::println);
Not a very convenient solution, but it might work for you.
A common technique is to track changes on the entity POJO.
Load Dog with color = black, size = null and age = null
Set size to null (the setter will mark this field as changed)
Run update SQL
The POJO will have an internal state knowning that size was changed, and thus include that field in the UPDATE. age, on the other hand, was never set, and is thus left unchanged. jOOQ works like that, I'm sure there's others.
Only what should be updated is sent. Problem: How can you explicitly empty / delete a field? If a "NULL" is passed in the JSON, we get NULL in the object, but any other field that is not passed is NULL as well. Therefore we cannot distinguish which field can be deleted and which field cannot be touched.
The problem you have identified is genuine; I have faced this too. I think it is reasonable to not provide a technical solution for this, but rather document the API usage to let the caller know the impact of leaving out a field or sending it as null. Of course, assuming that the validations on the server side are tight and ensure sanity.
The complete object is sent. Problem: Here the object could be fetched via a GET before, changed accordingly and returned via PUT. Now we get all information back and could write the information directly back into the database. Because empty fields were either already empty before or were cleared by the user.
This is "straighter-forward" and should be documented in the API.
What happens if the objects are extended by an update of the API.
With the onus put on the caller through the documentation, this too is handled implicitly.
Or do you have to create a separate endpoint for each field?
This, again, is a design issue, the solution to which varies from person-to-person. I would rather retain the API at a record level than at the level of individual value. However, there may be cases where they are needed to be that way. Eg, status updates.
Suppose we extend the database by five more fields. The user of the API makes a GET, gets the 15 fields, but can only read the 10 fields he knows on his page (because he hasn't updated his side yet). Then he changes some of the 10 fields and sends them back via PUT. We would then update only the 10 fields on our site and the 5 new fields would be emptied from the database.
So let's start with an example - what would happen on the web, where clients are interacting with your API via HTML rendered in browsers. The client would GET a form, and that form would have input controls for each of the fields. Client updates the fields in the form, submits it, and you apply those changes to your database.
When you want to extend the API to include more fields, you add those fields to the form. The client doesn't know about those fields. So what happens?
One way to manage this is that you make sure that you include in the form the correct default values for the new fields; then, if the client ignores the new fields, the correct value will be returned when the form is submitted.
More generally, the representations we exchange in our HTTP payloads are messages; if we want to support old clients, then we need the discipline of evolving the message schema in a backwards compatible way, and our clients have to be written with the understanding that the message schema may be extended with additional fields.
The person using the API does not know that there is a new field in JSON for the salary.
The same idea holds here - the new representation includes a field "salary" that the client doesn't know about, so it is the responsibility of the client to forward that data back to you unchanged, rather than just dropping it on the floor assuming it is unimportant.
There's a bunch of prior art on this from 15-20 years ago, because people writing messages in XML were facing exactly the same sort of problems. They have left some of their knowledge behind. The easiest way to find it is to search for some key phases; for instance must ignore or must forward.
See:
Versioning XML Vocabularies
Extensibility, XML Vocabularies, and XML Schema
Events in an event store have the same kinds of problems. Greg Young's book Versioning in an Event Sourced System covers a lot of the same ground (representations of events are also messages).
The accepted answer works well but it has a huge caveat which is that it's completely untyped. If the object's fields change then you'll have no compile time warning that you're looking for the wrong fields.
Therefore I would argue that it's better to force all fields to be present in the request body. Therefore a null means the user explicitly set it to null while if the user misses a field they'll receive a 400 Bad Request with the request body describing the error in detail.
Here's a great post on how to achieve this: Configure Jackson to throw an exception when a field is missing
Here's my example in Kotlin:
data class PlacementRequestDto(
val contentId: Int,
#param:JsonProperty(required = true)
val tlxPlacementId: Int?,
val keywords: List<Int>,
val placementAdFormats: List<Int>
)
Notice that the nullable field is marked as required. This way the user has to explicitly include it in the request body.
You can control empty or null values as below
public class Person{
#JsonInclude(JsonInclude.Include.NON_NULL)
private BigDecimal salary; // this will make sure salary can't be null or empty//
private String phone; //allow phone Number to be empty
// same logic for other fields
}
i) As you're updating weight and removing the phone number,Ask client to send fields which needs to updated along with record identifier i.e id in this case
{
"id" : 2,
"weight" :78,
"phone" : null
}
ii) As you're adding salary as one more column which is mandatory field & client should be aware of it..may be you have to redesign contract

How do i understand firebase query in android? [duplicate]

I have a list of numbers inside that have certain data, in which I am adding a value "Sí" or "No", as in this image:
The last "Sí" that I added was the one that is in number 4, but if I do a filter in Firebase with
.equalTo("Sí").limitToLast(1)
I return the value of "Sí" positioned in the number 5 and not in the 4 that was the last "Sí" that I added to the database. There is some way to recognize the last "Sí", without the need of that is in the last position of the list?
I still can not find the solution to this, I hope you can help me.
Thank you.
There is some way to recognize the last "Sí", without the need of that is in the last position of the list?
Yes there is. The most common approach would be to add under each object a new property of type Timestamp named lastUpdate and then query descending according to it. Everytime you update a value, change the value also to lastUpdate with the current timestamp. This is how your schema might look like:
Firebase-root
|
--- Users
|
--- 1
| |
| --- Activo: "Si"
| |
| --- lastUpdate: 1561202277
|
--- 2
|
--- Activo: "No"
|
--- lastUpdate: 1561202299
This is how to save the timestamp:
How to save the current date/time when I add new value to Firebase Realtime Database
And this is how to order descending:
Firebase Data Desc Sorting in Android
How to arrange firebase database data in ascending or descending order?

Why is a document from a Cloud Firestore database not deleted?

I have the following database structure:
Firestore-root
|
--- users (collection)
| |
| --- userId (document)
| |
| --- //user details
|
--- products (collection)
|
--- listId (document)
|
--- listProducts (collection)
|
--- productId
|
--- //product details
And I use the following code to delete the documents:
rootRef.collection("users").document(userId).delete(); //works fine
rootRef.collection("products").document(listId).delete();
The first line of code works perfectly but the second is not deleting the list document. Is it because it has a subcollection beneath it or am I doing something wrong?
How to delete the entire listId document together with everything it has beneath it?
Thanks!
Collections that are nested under a document, are not automatically deleted when you delete that document. When you only delete the document itself, the Firebase console will show the document ID in italic. The document itself isn't there anymore (you can see this when you select it, as it won't have any fields), but the nested collections are still accessible.
So I suspect that the products document is gone, but the nested collections still exist. As the Firestore documentation on deleting collection describes, you'll need to delete the documents from the collection to get rid of it.

How can i rename a branch/node in firebase

I have attached a picture, wherein i wish to rename the node during the run time for an application.
Short Answer:
Currently, Firebase doesn't allow you to rename a node/branch. So you might delete that node and create a new node again.
There is no api within Firebase for doing that. What can you do instead is to copy the information to another node and then simply delete the old one.
This is not a good practice to have the name of the product as a Firebase key. You need instead of using the product name, to use a unique identifier, an id. The best option is to use the unique key generated by the push() method. The name of the product will be then a child of your productId. Your database should look like this:
Firebase-root
|
--- prducts_details
|
--- -Ki-k6fM5GTRpQhGBRFRa
| |
| --- productName: "product1"
|
--- -Ki-oAAtTG1bWzLvKD5L
|
--- productName: "product2"
You have an option to Export the data. Once exported open and edit the exported.json. Here you can edit the node you want then save it and have Import to firebase db.

Categories