首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何检查Firebase查询结果是否为0

如何检查Firebase查询结果是否为0
EN

Stack Overflow用户
提问于 2019-05-31 02:08:31
回答 1查看 43关注 0票数 0

如何检查Fire基础查询结果是否为0

我正在开发一个登录页面-如果数据不存在,那么直接注册用户。但根据fire base返回的数据,如果结果为0,则插入fire base

代码语言:javascript
复制
if (!txt_phone.equals("")) {
    Query query = auth.orderByChild("phoneNo").equalTo(StringUtils.trim(txt_phone)).limitToFirst(1);
    query.addListenerForSingleValueEvent(
            new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User exisitngUser = null;
                    for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                        exisitngUser = snapshot.getValue(User.class);
                    }
                    Log.d(TAG, "Exisiting User" + exisitngUser.toString());

                    if (exisitngUser != null && exisitngUser.getPhoneNo() != null && exisitngUser.getPhoneNo().equalsIgnoreCase(txt_phone)) {
                        Log.i(TAG, "onDataChange Method");
                        addFlag[0] = true;
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("phoneNo", txt_phone);
                        intent.putExtra("userName", txt_userName);
                        startActivity(intent);
                        finish();
                    } else if (addFlag[0] = false) {
                        Log.i(TAG, "onDataChange Method else");
                        addFlag[0] = true;
                        addUser = FirebaseDatabase.getInstance().getReference("Messanger");
                        User login = new User("1", txt_userName, txt_password, txt_phone, null, "Active", null);
                        addUser.child("loginUser").push().setValue(login);
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent.putExtra("phoneNo", txt_phone);
                        intent.putExtra("userName", txt_userName);
                        startActivity(intent);
                        finish();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.w(TAG, "getUser:onCancelled", databaseError.toException());
                }
            });
//This is called before the data is returned, How to I block this until the data is checked
            if (!txt_phone.equals("") && addFlag[0] == false) {
                Log.i(TAG, "Out of onDataChange Method");
                addFlag[0] = true;
                addUser = FirebaseDatabase.getInstance().getReference("Message");
                User user = new User("1", txt_userName, txt_password, txt_phone, null, "Active", null);
                addUser.child("User").push().setValue(user);
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("phoneNo", txt_phone);
                intent.putExtra("userName", txt_userName);
                startActivity(intent);
                finish();
            }
        }
    }
});

这被称为新用户的第一个[!txt_phone.equals("") && addFlag[0] == false],甚至在返回查询结果之前也是如此。我需要它等待,直到从firebase返回结果,如果firebase返回0,那么这个if[!txt_phone.equals("") && addFlag[0] == false]应该被执行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-31 03:35:03

如果您指的是DataSnapshot,那么您可以随时调用hasChildren()方法,该方法将指示是否有任何查询结果与您的条件匹配。该方法返回一个指示结果的布尔值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56383472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档