I'm trying to do snapshot from two different child sections from database.
private String name;
snap1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
name = value
}
snap2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(value.equals(name)) {
//Do this
}
}
}
}
But after first snapshot is done, name
is null
. I see inside of onDataChange
name is not null. How can I store value into name and use it for the second datasnapshot?
Firebase calls are Asynch, you have to wait for the first call to end before calling the second one. I don't know about the perfect way to do this but I made a function and placed one of my call inside it and called that function inside the first call. You can do your work something like this:
private String name;
snap1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
name = value
secondCall();
}
}
//The function with second call
secondCall(){
snap2.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(value.equals(name)){
//Do this
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.