How to make history score in Array
I try u make score in array like this
this my firestore
And this is my code
String uid = auth.getCurrentUser().getUid();
muridref.document(uid).update("nilai", FieldValue.arrayUnion(skortampil));
When I get the same score the array field doesn't make new Array data,
without see data there or not in array
As mentioned in the documentation Update elements in an array about this behavior:
If your document contains an array field, you can use arrayUnion() and arrayRemove() to add and remove elements. arrayUnion() adds elements to an array but only elements not already present.
Considering that, it's working as expected, since it's not adding values that are equal. So, this means that you won't be able to add values that are equal using the method arrayUnion() directly.
This other question from the Community - accessible here - indicates that for you to achieve this goal, you will need to read all the values from the array in your client side, update your values in the array outside the database and then, writing/updating it back in the database.
Let me know if the information helped you!
Related
Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {joe#gmail.com, dan#gmail.com}, and I want to append newEmails = {mat#gmail.com, sharon#gmail.com} to it.
Problem
The only options I know for this are:
Reading the friendEmails array first, then adding the union of it and newEmails to the document.
Iterating over the elements of newEmails (let's and each iteration doing:
myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(Since FieldValue.arrayUnion only lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("mat#gmail.com", "sharon#gmail.com");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?
Thanks!
You can add multiple items to an array field like this using FieldValue.arrayUnion() as the value of a field update:
docRef.update("friendEmails", FieldValue.arrayUnion(email1, email2));
If you need to convert an ArrayList to an array for use with varargs arguments:
docRef.update("friendEmails", FieldValue.arrayUnion(
newEmails.toArray(new String[newEmails.size()])
));
I am new in android development and when I read array data from firestore using following code
val variable = arrayOf(document.get("restaurant"))
and then loop over the variable using code
varibale.forEach {
Log.d("someTag", ${it.toString()} + " is your data")
}
I get the result with square brackets at log as following
[somedata, somedata2] is your data
my problem is that forEach loop runs only once and I am not able to get the result (without square brackets) as following
somedata is your data
somedata2 is your data
I have 2 elements in my restaurant array in firestore
I will be very thankfull to any one who will help me.
You are actually wrapping an array/list into another array when using arrayOf, that's why you see those brackets. Instead, try casting your document.get("restaurant") and then looping directly through it.
arrayOf doesn't parse an array. It creates a new array using the elements you pass to it. That's not what you want. You should instead cast document.get("restaurant") to the type that you expect to get from Firestore.
If a field is an array of strings, then the SDK will give you a List<*>, and you will need to make sure each item in the list is a String, if that's what you stored in the array.
val variable = document.get("restaurant") as List<*>
// Iterate variable here, make sure to check or convert items to strings
E.g. I have next data in my Firebase Realtime Database (my json file contains json array of some objects):
I would like to get all objects starting from a specific index (position) to the end of this array (in Firebase DB json array is converted to simple object which contains children with keys 001, 002 and so on)
So I need something like this but for Firebase query:
list.subList(10, list.size)
I know there are limitToFirst, limitToLast methods but it's different
fireDatabaseReference.child("episodes").limitToLast(10)
It won't do what I need. Because I need to know the size of this array and I need to be sure that this array won't become bigger at the moment someone makes such request (at some point this array can get become bigger due to adding new objects)
Would be great to have a method like from to get all children from 10 to the end (so first 9 children are excluded):
fireDatabaseReference.child("episodes").from(10)
But there is no such method
Is there any solution for this?
Solved :)
fireDatabaseReference.child("episodes").orderByKey().startAt("10")
Suppose Type is the name of the array which contains A,B,C,D as elements and is created in strings.xml.Now in the form user can either select any of the elements or add new elements.Suppose user adds E,F,G .Now what i want to achieve is that anyhow the Type array have A,B,C,D,E,F,G in it. Using sqlite is done.But i want to save it in the Type array only and not anywhere else.Is it possible ?
Since anything that you define in res folder acquires a unique id in R.java, which is an array, and size of array cannot be changed at runtime.. So, it is not possible.
You can only save data to persistent storage like SQLite data base, shared preferences or a text file, each of which have different strengths and weaknesses and are suited for different situations, so take your pick.
I have a small issue with ArrayList. I have to fetch the document from the server.
The document contains 7 fields of data. I have to show the document names in the list view.
For this I have added different fields data to the different ArrayList. So when I click on the document name, based on the position of the document, I fetched the all fields data from the different Arraylist based on the position.
But Have a small issue using by using the above procedure. Is there any procedure that is not depend on the position, what I want is irrespective of position if I click on the Document,based on the keyword document data to be extract.
Any Help Appreciated.
Thanks in Advance.
I got your point. If you try to manage different ArrayLists then it would be difficult to manage it. I mean if you delete item from particular position from particular ArrayList then you will have to delete items from same position from other ArrayList, if you forgot to do so then it will be unbalanced.
Solution:
Instead feasible solution is to create ArrayList<Object> or ArrayList<HashMap<String,String>>, so your every item is the type of particular object, and every object contains detail and everything of particular items.
For example: ArrayList<Documents>, here ArrayList will contains list of Documents objects, and every objects contains values of 7 fields.
Its simply easy to define Documents class with getter/setter attributes.