4

I'm trying to use Firestore.

I have a collection Group which contains a List of users

    public class User {

    private String mail;
    private String username;

    public User() {
    }

    public User(String mail, String username) {
        this.mail = mail;
        this.username = username;
    }

    public String getMail() {
        return mail;
    }

    public String getUsername() {
        return username;
    }
}

I would like to update this list so I've tried :

List<User> list = new ArrayList<>();
        list.add(new User("mail@gmail.com", "foo"));
        reference.update("users", list)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "DocumentSnapshot successfully updated!");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error updating document", e);
                    }
                });
    }

But I have an exception :

java.lang.IllegalArgumentException: Invalid data. Unsupported type: my.package.name.models.User

How can I update a list of object using Firestore ?

Bob
  • 589
  • 1
  • 11
  • 25
  • You can't perform list operations on an array field in Firestore (in any language). You have to read the array, modify it locally, then write the array back out. – Doug Stevenson Jun 25 '18 at 21:44

1 Answers1

1

First of all, as documented update doesn't accept List.

Also, documented here that you can execute multiple write operations as a single batch that contains any combination of set(), update(), or delete() operations, using the WriteBatch.

amrro
  • 1,526
  • 17
  • 21