どのように私は、ドキュメント内のArrayListを更新することができますか?

ジャスティンTvarijonavičius:

私は更新したい場合にはrecipIds、ユーザーの内側に配列リストを配列リストを拭い取得し、新しい値が設定されている文書。

私は、古いコレクションを取得して、更新に新しい値を追加しようとしましたが、それはただ行き過ぎに思えました。

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);
}

私は古いリストデータを拭くことなく、新たな価値を持つ私の配列リストを更新します。

ここではFirestoreでどのように見えるかです:

https://imgur.com/a/LUnYxsa

編集されました:

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

編集しました

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()));
}

エラー:

java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported
        at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@18.2.0:293)
        at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@18.2.0:261)
        at com.google.firebase.firestore.UserDataConverter.convertAndParseFieldData(com.google.firebase:firebase-firestore@@18.2.0:202)
        at com.google.firebase.firestore.UserDataConverter.parseArrayTransformElements(com.google.firebase:firebase-firestore@@18.2.0:440)
        at com.google.firebase.firestore.UserDataConverter.parseSentinelFieldValue(com.google.firebase:firebase-firestore@@18.2.0:346)
        at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@18.2.0:248)
        at com.google.firebase.firestore.UserDataConverter.convertAndParseFieldData(com.google.firebase:firebase-firestore@@18.2.0:202)
        at com.google.firebase.firestore.UserDataConverter.parseUpdateData(com.google.firebase:firebase-firestore@@18.2.0:176)
        at com.google.firebase.firestore.DocumentReference.update(com.google.firebase:firebase-firestore@@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)
アレックス・マモ:

更新するにはrecipeIds、配列を、あなたが使用する必要がありますFieldValue.arrayUnion次のコード行のように:

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

編集:

新たな要素を追加することによって、例えば、配列ことが更新できるためには、に渡す必要があるarrayUnion()メソッド文字列(ID自体)。あなたはできませんリストや文字列の配列を渡します。あなたがそれを行うことができます方法はありません。あなたは、アレイ、一つ一つの要素を追加/削除することができます。

あなたが別の配列との配列を更新する場合は、まず、データベースから文書を取得し、必要な変更をクライアント側を作成し、その後、戻ってデータベースにそれを書く必要があります。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=236342&siteId=1