android - 如何通过一笔交易在 Firebase 上创建用户并上传数据

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

我正在制作一个网络应用程序,该应用程序使用电子邮件和密码在 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/53391078/

相关文章:

android - 如何使用蓝牙耳机录制声音

android - 具有 4 个数据库版本的 SQLite onUpgrade

android - 按下按钮时如何在 Activity 中绘制垂直线?

android - SQLite删除级联不起作用

amazon-s3 - 使用 Firebase 和 S3

firebase - 有没有办法向正在运行的 Firebase 模拟器添加/修改功能?

react-native - react native 内联文本链接

android - 尝试在空对象引用上调用虚拟方法 'boolean abi44_0_0...FabricViewStateManager.hasStateWrapper()'

javascript - Arrays.sort 比较器问题

ios - 如何检查 Firebase 中是否存在用户名