mobileprogramming
mobileprogramming

Reputation: 167

Android Firebase get value of child without DataChange

I want to get value of child. But I have to wait what data changed. But I don't want to get value without datachange. (without listener)

I use below method :

FirebaseDatabase.child("benim-degerim").addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot snapshot) {
    snapshot.getValue().toString()
  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
  }
});

I want to snapshot.getValue() without listener. How can I do it ?

Upvotes: 0

Views: 1382

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

Loading data over the internet takes time. That's why it's done asynchronously, so that the user of your app can continue using the app while the data is being downloaded.

Since there is no way to make the internet instant, downloads will always be asynchronous and thus require a listener (or for other frameworks, some other form of callback).

The fastest way I've found to get used to asynchronous methods is to reframe your problem from "first get data, then do something with it" to "when we get the data, do something with it". This typically means that you move the code that does "something" into the onDataChange() method.

Upvotes: 4

Related Questions