如果firebase网址不存在,我们会尝试添加一个侦听器,会发生什么情况? [英] What happens if a firebase url doesnot exist and we try to add a listener to it?
问题描述
我正在开发一个应用程序,要求用户输入城市和州名,并返回该地点的某个位置。但问题是,如果指定的州或城市不存在于我的Firebase中,那么当侦听器触发时,我的进度对话框会继续移动,而不会进一步触发任何操作。
以下是我的部分代码:
$ state =从用户获取
// city =从用户获取
mapProgress.show();
< ());();();} firebase.child(markers)。child(state).child(city).addChildEventListener(new ChildEventListener(){
@Override $ b $ public void onChildAdded(final DataSnapshot dataSnapshot,String s){
mapProgress.dismiss();
if(dataSnapshot.exists()){
//做某事
}
else
Toast.makeText(MainActivity.this,No data f ound!,Toast.LENGTH_SHORT).show();
$ b @Override
public void onChildChanged(DataSnapshot dataSnapshot,String s){
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot){
$ b @Override
public void onChildMoved(DataSnapshot dataSnapshot,String s){
}
@Override
public void onCancelled(FirebaseError firebaseError){
mapProgress.dismiss();
Toast.makeText(MainActivity.this,Cancelled,Toast.LENGTH_SHORT).show();
}
});
}
当添加,更改,移除或移动子项时,会调用code> ChildEventListener 。在一个空的位置,这些事件都不会发生,所以没有任何方法被调用。
A ValueEventListener
如果某个位置没有值,则调用一个空值。因此,如果您还调用 addListenerForSingleValueEvent()
,则可以检测到该位置没有值:
Firebase cityRef = firebase.child(markers)。child(state).child(city);
cityRef.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot snapshot){$ b $ if(snapshot.getValue()== null){
System.out.println(没有数据存在+ city);
}
}
@覆盖
public void onCancelled(FirebaseError firebaseError){
}
});
如果您想要在同一个调用中处理子元素,您可以在 onDataChange()
:
$ b $ pre $ public $ onDataChange(DataSnapshot snapshot){
(if)(snapshot.getValue()== null){
System.out.println(没有数据存在+城市);
$ {
for(DataSnapshot citySnapshot:snapshot.getChildren()){
//用citySnapshot.getValue()做一些事情
}
}
}
I am developing an application in which I ask the user to enter the city and state name and return certain location in that place. But the problem is that if the specified state or city doesn't exist in my Firebase then my Progress Dialog,which I am supposed to dismiss when the listener has triggered, keeps moving and no action is triggered further.
Here is that portion of my code:
//state = get from user //city = get from user
mapProgress.show();
firebase.child("markers").child(state).child(city).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(final DataSnapshot dataSnapshot, String s) {
mapProgress.dismiss();
if (dataSnapshot.exists()) {
//do something
}
else
Toast.makeText(MainActivity.this, "No data found!", Toast.LENGTH_SHORT).show();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
mapProgress.dismiss();
Toast.makeText(MainActivity.this, "Cancelled.", Toast.LENGTH_SHORT).show();
}
});
}`
The methods in a ChildEventListener
are called when a child is added, changed, removed or moved. On an empty location, none of these events happen, so none of the methods are called.
A ValueEventListener
will be called with an empty value if no value exists at a location. So if you also call addListenerForSingleValueEvent()
, you can detect that the location has no value:
Firebase cityRef = firebase.child("markers").child(state).child(city);
cityRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.getValue() == null) {
System.out.println("No data exists for "+city);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
If you want to handle the children in the same call, you can just loop over them in onDataChange()
:
public void onDataChange(DataSnapshot snapshot) {
if (snapshot.getValue() == null) {
System.out.println("No data exists for "+city);
}
else {
for (DataSnapshot citySnapshot: snapshot.getChildren()) {
// Do something with citySnapshot.getValue()
}
}
}
这篇关于如果firebase网址不存在,我们会尝试添加一个侦听器,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!