Firebase android count children and store it in a variable

后端 未结 4 1993
感情败类
感情败类 2020-12-19 23:49

In my application I\'m able to get children count by datasnapshot.getchildren(). But what should I do if I want to store that count in a variable?

mRefReg.ad         


        
4条回答
  •  盖世英雄少女心
    2020-12-20 00:06

    There's some info missing in your question, but I think I know what's going on. Listeners in Firebase are asynchronous, so you cannot set a variable that easy.

    Options to go:

    1) Declare your variable inside the onDataChange method.

    2) Only use the variable when onDataChange method has completed.

    3) Pass a callback function in the method where you have declared your listener.

提交回复
热议问题