Android Studio Firebase数据库在添加新值之前等待读取 [英] Android Studio firebase database wait for read before adding new values

查看:118
本文介绍了Android Studio Firebase数据库在添加新值之前等待读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,用于存储用户,我想在添加新用户之前检查用户是否存在,以免覆盖.

I have a database in which I store users and I want to check if a user exists before adding a new one so I don't overwrite.

我有一个遍历数据库记录并返回布尔值(如果找到或未找到用户)的函数.

I have a function that goes through database records and returns a boolean value if it finds or doesn't find the user.

public boolean checkUserExists(final String emailAddress, final String emailDomain){
    DatabaseReference myRef = database.getReference("Users");

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot mydata : dataSnapshot.getChildren()){
                User user = mydata.getValue(User.class);

                if (user.getEmailAddress().equals(emailAddress) &&
                        user.getEmailDomain().equals(emailDomain)){
                    userExists = true;
                    break;
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    return userExists;
}

我当前尝试进行检查的方式是这样的:

The way I am currently trying to do the check is like this:

if (!(registerRepo.checkUserExists(emailAddress, emailDomain))){
                User user = new User(firsName, lastName, emailAddress, emailDomain);
                registerRepo.writeUser(user);
            } else {
                Toast toast = Toast.makeText(getBaseContext(), "User exists", Toast.LENGTH_SHORT);
                toast.show();
            }

问题在于它不等待读取就继续创建新记录(我使用的是push,因此它在新的push ID下创建了相同的记录).我已经看到firebase有一个叫做事务处理程序的东西,我认为这是我需要使用的东西,但是文档并没有帮助我,我看过其他人在这里问类似的问题,但是不能找出一个解决方案,所以,请您解释一下如何做,不要将我重定向到其他问题,我将不胜感激.

The problem is that it doesn't wait for the read and goes ahead and creates a new record (I'm using push so it creates the same record under a new push ID). I've seen that firebase has such a thing called transaction handler and I think that is what I need to use but the documentation didn't help me and I've looked at others asking sort-of the same question here but couldn't figure out a solution so please, if you can explain how to do it and not redirect me to other question I'd be grateful.

推荐答案

Firebase请求是异步的.

Firebase requests are asynchronous.

因此,如果要在从数据库获取结果之后执行一些代码,则需要在checkUserExists中添加回调.

So you need to add a callback in your checkUserExists if you want to do some code after getting the result from database.

例如:

public interface OnCheckUserExist {
    void exist();
    void notExist();
}

registerRepo.checkUserExists(emailAddress, emailDomain, new OnCheckUserExist(){
   @Override
   public void exist(){
       Toast toast = Toast.makeText(getBaseContext(), "User exists",Toast.LENGTH_SHORT);
       toast.show();
   }
   @Override
   public void notExist(){
       User user = new User(firsName, lastName, emailAddress, emailDomain);
       registerRepo.writeUser(user);
   }
})


public void checkUserExists(final String emailAddress, final String emailDomain, OnCheckUserExist onCheckUserExist){
    DatabaseReference myRef = database.getReference("Users");
    myRef.addValueEventListener(new ValueEventListener() {
        boolean userExist;

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot mydata : dataSnapshot.getChildren()){
                User user = mydata.getValue(User.class);

                if (user.getEmailAddress().equals(emailAddress) &&
                        user.getEmailDomain().equals(emailDomain)){
                    onCheckUserExist.exist();
                    userExist = true;
                    break;
                }
            }
            if (!userExist){
              onCheckUserExist.notExist();
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

这篇关于Android Studio Firebase数据库在添加新值之前等待读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆