我正在尝试从Firebase Realtime数据库中获取数据,并且数据检索工作正常,但是存在一些延迟。我想知道数据是否返回,并且代码中是否存在导致我遇到延迟的东西。
数据库:
代码:
private void Get_Rank ( )
{
for ( int ind = 0 ; ind < Child_One.size ( ) ; ind++ )
{
DatabaseReference databaseReference = FirebaseDatabase.getInstance ( ).getReference ( );
Query lastQuery = databaseReference.child ( "voting" ).child ( Child_One.get ( ind ) );
lastQuery.addListenerForSingleValueEvent ( new ValueEventListener ( )
{
double total20 = 0.0, total60 = 0.0;
double count20 = 0, count60 = 0;
@Override
public void onDataChange ( DataSnapshot dataSnapshot )
{
for ( DataSnapshot data : dataSnapshot.getChildren ( ) )
{
if ( data.child ( "type" ).getValue ( ).toString ( ).equals ( "AUD" ) || data.child ( "type" ).getValue (
).toString ( ).equals ( "CEO" ) )
{
total20 += Double.parseDouble ( data.child ( "rating" ).getValue ( ).toString ( ) );
count20 += 1;
}
else
{
total60 += Double.parseDouble ( data.child ( "rating" ).getValue ( ).toString ( ) );
count60 += 1;
}
}
double rate20 = ( total20 / count20 ) * 0.400;
double rate60 = ( total60 / count60 ) * 0.600;
double final_rate = rate20 + rate60;
Final_Rate.add ( final_rate );
}
@Override
public void onCancelled ( DatabaseError databaseError )
{
Toast.makeText ( getApplicationContext () , "Error" , Toast.LENGTH_SHORT ).show ( );
}
} );
}
System.err.println ( String.valueOf ( Final_Rate.size ( ) ) );
System.err.println ( String.valueOf ( Final_Rate.toString ( ) ) );
System.err.println ( String.valueOf ( Final_Rate.toString ( ) ) );
System.err.println ( String.valueOf ( Final_Rate.toString ( ) ) );
}
打印日志如下所示(单击3按钮后):
W/System.err: 0
[]
W/System.err: []
[]
W/System.err: 0
[]
[]
[]
W/System.err: 2
[3.86, 2.4000000000000004]
[3.86, 2.4000000000000004]
[3.86, 2.4000000000000004]
全部。