Is there a way to map my document id with toObject? - android

I'm deserializing an entire document, to keep things in handy and prevent me to check for each value and construct the object I use
myList.add(documentSnapshot.toObject(House::class.java))
Now, lets say House is this
data class House(val name:String,val:address:String)
Now, if I want to also get the House document Id and put it inside my document I do this
data class House(val houseId:String,val name:String,val:address:String)
But after doing that , the first line of code transforms into this
val houseId = documentSnapshot.id
val houseName = docuementSnapshot.getString("name")
val houseAddress = documentSnapshot.getString("address")
myList.add(House(houseId,houseName,houseAddress))
What I want to do is use .toObject() to also map that extra field that is the document id inside of it because if the House object expands in size, I will need to hand write again each property, and now think that house has 100 properties and I just need the id of it inside the object. I will need to write off 99 get fields to just place the document Id inside that house object.
Is there a way to map that id to the object without doing the above and just placing .toObject ?
Thanks

You need just add annotation #DocumentId
data class House(#DocumentId val houseId:String,val name:String,val:address:String)

What I want to do is use .toObject() to also map that extra field that is the document id
This will be possible only if the document already contains the id property that holds the document id as a value. If you only have the name and the address, then you cannot map the id, because it doesn't exist in the document. To be able to map all those three properties you should update each and every document in that collection so it contains the document id. If you have lots of documents in the collection, I recommend you use batch writes.
Right after that, you'll be able to use:
val house = documentSnapshot.toObject(House::class.java)
And now this house object will contain the id too.

Related

How to add data to the firebase firestore by generating different document id after hitting submit button again and again? Android Kotlin

I am trying to push the form data to the firebase-firestore. And I also did it successfully. But, the problem is that whenever I am trying to submit the form data again and again it just updates the last data with the current data.
Actually, my requirement is that whenever the user hit the submit button. It creates a document with a random id and stores the all data into that specific id that is generated.
You are specifying the document ID in .document() so it'll overwrite the same document. If you want a document with a random ID on every click, try using add() instead as shown below:
val collectionRef = FirebaseFirestore.getInstance().collection("Maintainance")
collectionRef.add(user).addOnCompleteListener(...)
Alternatively, you can leave .document() empty to get a DocumentReference with a random ID:
val userDocument = FirebaseFirestore.getInstance().collection("Maintanance").document() // <-- don't pass an ID
In addition to #Dharmaraj answer:
CASE_1: In a case where you need to track each user's all submitted forms, probably from your explanation you may need to organize each user's form.
Therefore if you need to organize each user's form then create another sub-collection [example: document(userId).collection("USER_FORMS")] within userID document like this:
val documentRef = FirebaseFirestore.getInstance().collection("Maintainance").document(UserUtils.user?.id.toString()).collection("USER_FROMS").document();
CASE_2 : In a case where you need to make your own custom document ID:
1- make a random number or string or any other data type.
2- The random number/string variable must be local to the code block/method that will execute the form submision function.
3- use the number/string generated as the form document Id like this:
//This must be local so as user clicks submision button so as it generates new random number;
val randomFormId = "generateThenumberOrStringAndInitializeTheVariable";
Then use the random number as the form document Id like this:
val documentRef = FirebaseFirestore.getInstance().collection("Maintainance").document(UserUtils.user?.id.toString()).collection("USER_FROMS").document(randomFormId);

How to get a value from map in kotlin?

Is it possible to get the values in a map associated to a key, without using the key?
I have this code.
val map = prices.associateBy({it.productName},{it.productPrice})
which gives me this
{Coffee=3.0, Gum=0.5, Beer=18.0}
I want to be able to just grab all the prices (3.0, 0.5, etc.) and save them to a list.
Any help is appreciated!
So to get all the values from a map, you can just use the built-in values property on the map like this.
val map = prices.associateBy({it.productName},{it.productPrice})
val values = map.values
It returns a read-only Collection of all values in this map. Note that this collection may contain duplicate values.

How to add a specific key with a specific value in a firebase database with Android?

I have a realtive simple Firebase database, in which i have 2 models. A ListModel and a UserModel. In my Lists, i'm using push() method to generate unique ids. Each unique id i want to be added as a key and "true" as it's value under Users/gmail#gmail,com/Lists.
When i add the first list, the database looks like this:
And everything works fine, but when i try to add another one, the database looks like this:
In Users/gmail#gmail,com/Lists, the first one is overwritten by the second insert. How can i add the specific id and the specific value, as a new item as shown below?
And this is my code:
final UserModel um = new UserModel();
um.setUserEmail("gmail#gmail,com");
userDatabaseReference.setValue(um);
ListModel lm = new ListModel();
lm.setListName(listName);
listKeyDatabaseReference = listDatabaseReference.push();
listKey = listKeyDatabaseReference.getKey();
listKeyDatabaseReference.setValue(lm);
listDatabaseReference.child(listKey).child("Users").child("gmail#gmail,com").setValue("true");
userDatabaseReference.child("gmail#gmail,com").child("Lists").child(listKey).setValue("true");
Thanks in advance!
Check the official doc:
For basic write operations, you can use setValue() to save data to a specified reference, replacing any existing data at that path.
Your problem is here:
userDatabaseReference.setValue(um);
In this way you are overriding all children in the userDatabaseReference path.
It means that the first record in Users/gmail#gmail,com/Lists is just deleted when you are adding the second one.
Before using the
userDatabaseReference.setValue(um);
you can check if the record exists.
If doesn't exist use the setValue to add the user-model with all its data.
If it exists, just skip this step and add the data in the lists path inside the same user.

How to read nodes that have map values only in firebase

I'm listening to a node that has children with different value types, like in te picture below
How can I get the values of the nodes that contained map values indicated with red rectangle only ?
I tried to use java class containing the fields in the red rectangle, and an error shows and says "Cannot convert type String to MyObject". So I think its reading the children that have string value first!
I really think you should restructure your data to have a separate node just for the data you want to receive.

Android get data from the parse table (json file) (imported in parse.com-data browser)

I have one json file, that I imported in parse.com->data browser->import partition. I can get the String and image value from the table but I have no idea about how to get following values. First column have name chapters of type array look like one field following,
[{"__type":"Pointer","className":"Chapter","objectId":"BCr3uAnapV"}]
how to get above value and second column have, name user of type object look like following,
{"password":"xxx"}
Please any one help me for above, I have tried Googling but it did not help.
EDIT:
See following screen shot: arrow display column 1) chapter and have another column 2) user I want to fetch (get ) that for e.g. if we want to get data of String type like: String provider = (String) objectList.get(i).get("provider"); this manner this way I want to above data, below my screen shot:
This line is a relation (of type Pointer) to another Class in Parse:
[{"__type":"Pointer","className":"Chapter","objectId":"BCr3uAnapV"}]
The class being Chapter. You can make sure this object is fetched together with the object pointing to it using "include":
query.include("chapter");
When you query for the other object, using query.include on pointer relations will ensure these related objects are fetched as well.

Categories

Resources