Ad

How Can I Update ArrayList Inside Document?

When I want to update recipIds array list inside users document the array list gets wiped and new value is set.

I tried to get the old collection then add new value to it and update but that just seemed overkill.

public void saveRecipeIds(User user) {
    String currentUserId = mAuth.getCurrentUser().getUid();

    Map<String, Object> userMap = new HashMap<>();
    userMap.put("recipeIds", user.getRecipesIds());

    db.collection("users").document(currentUserId).update(userMap);
}

I want to update my array list with new value without wiping old list data.

Here is how it looks in Firestore:

https://imgur.com/a/LUnYxsa

Edited:

public void saveRecipeIds(User user) {
    String currentUserId = mAuth.getCurrentUser().getUid();

    Map<String, Object> userMap = new HashMap<>();
    userMap.put("recipeIds", user.getRecipesIds());

    db.collection("users").document(currentUserId)
        .update("recipeIds", FieldValue.arrayUnion(userMap));
}

https://imgur.com/a/yTywInK

Edited

public void saveRecipeIds(User user) {
    String currentUserId = mAuth.getCurrentUser().getUid();

//  Map<String, Object> userMap = new HashMap<>();
//  userMap.put("recipeIds", user.getRecipesIds());

    db.collection("users").document(currentUserId)
        .update("recipeIds", FieldValue.arrayUnion(user.getRecipesIds()));
}

error:

java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported
        at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:[email protected]@18.2.0:293)
        at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:[email protected]@18.2.0:261)
        at com.google.firebase.firestore.UserDataConverter.convertAndParseFieldData(com.google.firebase:[email protected]@18.2.0:202)
        at com.google.firebase.firestore.UserDataConverter.parseArrayTransformElements(com.google.firebase:[email protected]@18.2.0:440)
        at com.google.firebase.firestore.UserDataConverter.parseSentinelFieldValue(com.google.firebase:[email protected]@18.2.0:346)
        at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:[email protected]@18.2.0:248)
        at com.google.firebase.firestore.UserDataConverter.convertAndParseFieldData(com.google.firebase:[email protected]@18.2.0:202)
        at com.google.firebase.firestore.UserDataConverter.parseUpdateData(com.google.firebase:[email protected]@18.2.0:176)
        at com.google.firebase.firestore.DocumentReference.update(com.google.firebase:[email protected]@18.2.0:213)
        at com.example.cookingapp.Repository.UserRepository.saveRecipeIds(UserRepository.java:143)
        at com.example.cookingapp.ViewModels.UserViewModel.saveRecipeIds(UserViewModel.java:57)
        at com.example.cookingapp.Views.Fragments.Home.FinishRecipeFragment$1.onClick(FinishRecipeFragment.java:111)
        at android.view.View.performClick(View.java:6199)
        at android.widget.TextView.performClick(TextView.java:11090)
        at android.view.View$PerformClick.run(View.java:23647)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Ad

Answer

To update the recipeIds array, you should use FieldValue.arrayUnion like in the following line of code:

db.collection("users").document(currentUserId)
    .update("recipeIds", FieldValue.arrayUnion("newRecipeId"));

Edit:

In order to be able update an array, for instance by adding a new element, you need to pass to the arrayUnion() method a String (the id itself). You cannot pass a list or an array of strings. There is no way you can do that. You can add/remove an element from an array, one by one.

If you want to update the array with another array, you should first get the document from the database, make the necessary changes client side and then write it back into the database.

Ad
source: stackoverflow.com
Ad