gpt4 book ai didi

android - 在 firebase android 中检索数据时卡住

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:19 24 4
gpt4 key购买 nike

运行此程序时,我总是卡在此处:databaseRefer.addValueEventListener(new ValueEventListener() 我确实尝试了不同的方法,但结果相同。

我正在尝试读取该节点以检查该用户名是否已存在。

public void validateUsername(final String sUsername, final FirebaseUser user) {

DatabaseReference databaseRefer;
FirebaseDatabase mFirebaseInstance;

mFirebaseInstance = FirebaseDatabase.getInstance();

databaseRefer = mFirebaseInstance.getReference().child("TakenUserNames").child(sUsername);
databaseRefer.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
// username is already taken
Toast.makeText(SignupActivity.this, "Username already exist. try a different one", Toast.LENGTH_SHORT).show();
} else {
// username is valid
onAuthenticationSucess(user);
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SignupActivity.this, "Connection Error. Please try again in some time.", Toast.LENGTH_SHORT).show();
}
});
}

enter image description here

最佳答案

试试下面的代码

public void validateUsername(final String sUsername) {
DatabaseReference databaseRefer;
FirebaseDatabase mFirebaseInstance;

mFirebaseInstance = FirebaseDatabase.getInstance();

databaseRefer = mFirebaseInstance.getReference().child("TakenUserNames").child(sUsername);
databaseRefer.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
// username is already taken
Toast.makeText(SignupActivity.this, "Username already exist. try a different one", Toast.LENGTH_SHORT).show();
} else {
// username is valid
onAuthenticationSucess(task.getResult().getUser());
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SignupActivity.this, "Connection Error. Please try again in some time.", Toast.LENGTH_SHORT).show();
}
});
}

注意:- 当您调用 Firebase API 时,该 API 将在单独的线程上运行。因此,在 Firebase 调用之后的函数中,您将从 bool 数组返回值。但 Firebase API 调用尚未完成。因此,请尝试在 onDataChange() 中执行或运行您的代码。

关于android - 在 firebase android 中检索数据时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com