I am trying to create a method to read my Firebase database in line with the rest of my code. I am trying to use a Reentrant Lock to do this, but it is not working. I want the output to be
---------- A
---------- B
---------- C
---------- D
upon calling updateMerchants(). Is there anyway I can make this work?
public static void updateMerchants()
{
System.out.println("---------- A");
lock.lock();
DatabaseReference database = FirebaseDatabase.getInstance().getReference("users").child("merchants");
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
System.out.println("---------- B");
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.exists()) {
System.out.println("----------exists");
}
}
lock.unlock();
System.out.println("---------- C");
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("onCancelled: ", databaseError.toString());
}
});
updateMerchantsUtil();
}
private static void updateMerchantsUtil()
{
lock.lock();
System.out.println("---------- D");
lock.unlock();
}