Android Firebase在没有DataChange的情况下获得孩子的价值 [英] Android Firebase get value of child without DataChange

查看:44
本文介绍了Android Firebase在没有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)

我使用以下方法:

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

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

我想要没有监听器的snapshot.getValue().我该怎么办?

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

推荐答案

通过Internet加载数据需要时间.这就是为什么它异步完成的原因,以便您的应用程序用户可以在下载数据时继续使用该应用程序.

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.

由于没有办法使Internet即时生效,因此下载将始终是异步的,因此需要侦听器(对于其他框架,则需要某种其他形式的回调).

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).

我发现习惯于异步方法的最快方法是将问题从首先获取数据,然后对其进行处理"重构为当我们获取数据时,对其进行处理".这通常意味着您将执行某些操作"的代码移到onDataChange()方法中.

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.

这篇关于Android Firebase在没有DataChange的情况下获得孩子的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆