android - 如何在一次交易中在 Firebase 上创建用户和上传数据

标签 android firebase react-native firebase-realtime-database firebase-authentication

我正在制作一个 Web 应用程序,它使用电子邮件和密码在 Firebase 上创建一个用户 firebase.auth().createUserUsingEmailAndPassowrd()

创建帐户后,我立即根据 firebase 数据库中的表单值上传一些数据。

一切正常,但有时,在最坏的情况下,用户已创建,但由于网络问题,数据未上传。并且该应用程序已被重定向到它需要该数据的地方,并且整个应用程序都失败了。

我如何确保仅当我尝试上传到数据库的数据也更新时才创建用户帐户。

总结:确保一些数据与用户创建一起上传,否则根本不会创建用户。

最佳答案

因为您非常害怕最坏的情况(这绝对是正常的),这里有一种方法可以做到:假设您有这些字段:电子邮件、密码和地址。地址字段是您需要存储在数据库中的字段(当然还有用户 uid)。

首先你尝试注册用户,然后点赞Faizy said , 你像这样使用 onCompleteListener

mAuth.createUserWithEmailAndPassword(email, password)
    .onCompleteListener(... {
        ... onComplete (... task) {
            if (task.isSuccessful()) {
                doInsertTheData();
            }
...

当任务成功时,你想将数据插入数据库,doInsertTheData() 是为

private void doInsertTheData() {
     // I believe you familiar with Firebase database inserting method, so I skip the long part
     FirebaseDatabase...setValue(address, new CompletionListener() {
         ... onComplete(FirebaseError firebaseError, ...) {
             if (firebaseError == null) {
                 // if your code reach here, its a happy ending
             } else {
                 // if it reach here, then you can remove the signed in user
                 // and tell the user that their registration is failed and should try again
                 FirebaseAuth.getInstance().getCurrentUser().delete();
             }

好了,只要数据库保存过程失败,用户凭证就会被删除

Note: I actually have another method in mind when I type this, but it's too complicated and I think this one is more easy to understand (I haven't try/use this though)

关于android - 如何在一次交易中在 Firebase 上创建用户和上传数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43028090/

相关文章:

android - 奇怪的跳跃在 Android MotionEvent 触摸位置

java - 如何以字节数组形式从服务器中的文件读取数据

javascript - 设置范围后启动 svg 动画 - angularjs - svg

javascript - 当我将图像上传到 Firebase 存储时,如何制作进度条?

android - RNFetchBlob.android.actionViewIntent 不适用于 android 11

android - index.android.bundle 是 android 版本发布所必需的吗?

android - Cordova Android 升级 - 找不到模块 ‘lodash/object/assign’ 错误

android - 链接以从 xml 字符串资源中的 android 应用程序发送电子邮件

firebase - 如何设置Firebase数据库规则?如何防止已删除用户的.write

android - 应用程序在升级targetSdk版本时崩溃