Reputation: 52
I was looking in the following google documentation of firestore and especially on the part of updating an array. they mention:
Update elements in an array 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. arrayRemove() removes all instances of each given element.
problem is, Android studio can't resolve the method arrayUnion. relevant part of the code:
public void onClick(View view) {
if(isStringValid(newListName.getText().toString())){
currListName = newListName.getText().toString().trim();
itemList mList = new itemList(currListName , mAuth.getCurrentUser().getEmail());
Map<String , Object> listMap = new HashMap<>();
listMap.put(KEY_LIST_NAME , currListName);
listMap.put(KEY_OWNER , mAuth.getCurrentUser().getEmail());
listMap.put(KEY_HAS_ACCESS , Arrays.asList(mAuth.getCurrentUser().getEmail()));
userRef.update("hasAccess" , FieldValue.arrayUnion(currListName));
gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
implementation 'com.android.support:support-v4:28.0.0-rc02'
implementation 'com.android.support:design:28.0.0-rc01'
implementation 'com.google.firebase:firebase-core:16.0.3'
implementation 'com.google.firebase:firebase-auth:16.0.3'
implementation 'com.google.firebase:firebase-firestore:17.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.android.gms:play-services-auth:16.0.0'
}
their code:
DocumentReference washingtonRef = db.collection("cities").document("DC");
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));
Upvotes: 1
Views: 1276
Reputation: 95
I found two FieldValue classes in Firebase Firestore SDK (21.0.0):
com.google.firebase.firestore.model.value.FieldValue
com.google.firebase.firestore.FieldValue
The first has no arrayUnion method, and second has it. Check what class is imported in your code file.
Upvotes: 2
Reputation: 317760
According to the release notes, array operations are supported starting with client SDK version 17.0.5. Here's a link to the release notes.
An any case, update your client SDK to use the latest Firestore APIs.
Upvotes: 0