【发布时间】:2019-02-09 03:10:21
【问题描述】:
我正在查看firestore 的以下谷歌文档,尤其是在更新数组方面。他们提到:
更新数组中的元素 如果您的文档包含一个数组字段,您可以使用 arrayUnion() 和 arrayRemove() 添加和删除元素。 arrayUnion() 将元素添加到数组中,但只添加不存在的元素。 arrayRemove() 删除每个给定元素的所有实例。
问题是,Android Studio 无法解析方法 arrayUnion。 相关部分代码:
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));
分级:
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'
}
他们的代码:
DocumentReference washingtonRef = db.collection("cities").document("DC");
washingtonRef.update("regions", FieldValue.arrayUnion("greater_virginia"));
【问题讨论】:
标签: android google-cloud-firestore