How to get the name of child realtime database kotlin - android

I am trying to get the email (in this case mikeyreid2002#gmail.com) however I don't see any method I can use that gets me the name of the child instead of the value.
https://i.stack.imgur.com/uzonn.png

In this case the email is the key of a nested object. You need to iterate over the collection documents and use their id as email
firestore.collection("Users").get().result.documents.forEach {
Log.d("Key", "${it.key}")
}
You can also use higher-order function to transform the documents, associate (using the email) and deserialize the data.

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.

Removing map elements from Google Cloud Firestore

I'm trying to remove a map<> from Google Cloud Firestore.
I use the following code for updating in Firestore
fun updateData(userId: String, values: Map<String, Any>) =
db.collection(COLLECTION_DATA).document(Id).update(values)
The document contains the used id's generated for google sign in and one of the field in the document is a map<Key,Value> . I'm trying to delete the content of the Field (removing a specific <key,value> pair)
I pass (userId, mapOf("FieldName.${Key}" to FieldValue.arrayRemove(Value))) as the parameter while calling the above firestore function.
But after updating the value in Firestore, the key is left out and the values are cleared.
If I pass (userId, mapOf("FieldName" to FieldValue.arrayRemove(Key))) as the parameter, then the whole map<> is removed (which is what I want) but the data structure of the Field is converted as an array after performing the update.
FieldValue.arrayRemove() is for array type fields only. It won't work here, because you're not actually using any arrays or lists.
You will just want to use FieldValue.delete() to remove the any map key and its value.
updateData(userId, mapOf("FieldName.${Key}" to FieldValue.delete()))

Firebase: How to access a child by passing the key?

questions
-KkXXZkVBjaNwnnPtRzU
date:
"19052017"
question:
"Q9"
uid:
"WAX6aezrBzXuXW3RtnnSApMazRb2"
This is the database structure at the moment. I'm writing a code to delete the node based on the submitted key, in this case KkXXZkVBjaNwnnPtRzU. I've obtained the key using the getKey() method and now I want to use the removeValue() on an DatabaseReference object to which I'm passing the key as the reference.
How would I go about doing that?
I should mention that the current key value I'm passing is a string. Is something like this possible at all? Basically the same principle as passing the value to a child() method, but instead of accessing a child of the node, I'd go for its key.
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference("questions");
myRef.child("KkXXZkVBjaNwnnPtRzU").removeValue();
It's simple as that. Of course, instead of hardcoding the values, you should use String which you've obtained previously.

Preset object Id when pushing to Firebase

I'm using a SortedList to sort my items inside the RecyclerView.
So I need to compare them by some unique Id, and Firebase provides awesome generated keys for that.
To push new value to a database I need to run:
public void addAdvert(Advert advert, String userId) {
String id = reference.child(NODE_USERS)
.child(userId)
.child(NODE_ITEMS)
.push()
.getKey() //execute this to just get the id
advert.setId(id);
getCurrentItemNode().child(advert.getId()).setValue(advert);
}
now my Advert item in database is fully equiped with Id and node key.
But I' curious, can I just write something like
reference.child(NODE_USERS)
.child(userId)
.child(NODE_ITEMS).push().setValue(advert);
And achieve the same result: both key and ID are set in database.
If you want to store the key in the item, you'll either have to perform two write operations or first generate the key and then write it.
But why do you want to store the key in the item to begin with?
As a side note, this code:
String id = reference.child(NODE_USERS)
.child(userId)
.child(NODE_ITEMS)
.push()
.getKey()
Is exactly the same as this:
String id = reference.push().getKey()
Firebase's push IDs are client-side generated, statistically guaranteed to be unique values. They're not dependent on the path and there is no round-trip to the database needed to determine these values.
See this blog post for more information: The 2^120 Ways to Ensure Unique Identifiers.

Query greater/less than using query#startAt/endAt

I am using query#startAt and query#endAt to retrieve data from Firebase
The problem with both those API is they retrieve nodes with greater/less than or equal key.
Is there anyway i can query greater-than or less-than only? Or i have to remove the item manually when i've done retrieving
I am using the Android-Java Firebase API
The operations are called startAt and endAt because they include the elements at the end points. To remove those "edge elements" you will either have to start at the next value/end at the previous value, or filter client-side.

Categories

Resources