【发布时间】:2018-03-29 20:45:52
【问题描述】:
请注意,以下场景使用 Cloud Firestore。
在我的云函数index.js 中,我有一个函数createUserAccount,它将新创建的用户添加到users 集合中,并为他们的电子邮件、照片、收藏夹设置字段(这是应用程序中的一个功能不需要详细说明),以及用户的其他一些不同的字段。该函数在成功完成以下代码sn-p时触发:
// The following method has been simplified to focus on the problem statement
public void createAccountWithEmailAndPassword(String username, String displayName,
String someOtherImportantThing) {
mFirebaseAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(getActivity(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "createUserWithEmail:Success");
onSuccess.updateAccount(displayName, username);
updateUsernameProperty(username);
onBackPressed();
} else {
if (task.getException() != null) {
if (task.getException()
.getMessage()
.contains(EMAIL_EXISTS_ERROR)) {
// Handle Email Exists Error
}
}
Log.w(TAG, "createUserWithEmail:failure",
task.getException());
}
});
}
如您所见,当任务成功完成时,会调用几个方法:updateUsernameProperty(String username) 和 updateAccount(String displayName, username)
updateAccount(String displayName) 获取用户选择的 displayName 并将其作为字段添加到用户帐户。 这不会更改用户集合 - 只会更改经过身份验证的帐户。
updateUsernameProperty(String username) 获取用户选择的用户名并将此属性设置为用户文档中的字段。这是代码示例,因为它可能有助于理解我的问题:
private void updateUsernameProperty(String username) {
DocumentReference docRef = mFirestore
.collection(TABLE_USERS)
.document(mFirebaseAuth.getUid());
docRef.update(USERNAME_KEY, username)
.addOnSuccessListener(aVoid -> Log.d(TAG, "Username DocumentSnapshot Successful"))
.addOnFailureListener(e -> Log.e(TAG, "Error Updating Document: user/uid/username", e));
}
(集合的典型路径是“users/{userId}/username”)
问题:尝试更新用户文档中的用户名字段时,云函数createUserAccount 尚未完成创建/添加新用户文档到数据库(即异步问题) .
我考虑过使用 rxJava2 来帮助缓解这个问题,但我似乎无法理解如何做到这一点。任何帮助是极大的赞赏。
如果您有什么需要澄清的,我会尽力提供所需的信息。
【问题讨论】:
标签: android firebase google-cloud-functions google-cloud-firestore