Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gouravd/fcf1a9fff4a24e821740e1ee92a153c8 to your computer and use it in GitHub Desktop.
Save gouravd/fcf1a9fff4a24e821740e1ee92a153c8 to your computer and use it in GitHub Desktop.
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.concurrent.CountDownLatch;
/**
* File Created by gouravd on 17/08/16.
*/
public class LoadFBDataSynchronously {
public static <T> T loadSynchronous(DatabaseReference databaseReference, Class<T> clazz) {
final DataSnapshotWrapper snapshotWrapper = new DataSnapshotWrapper();
final CountDownLatch latch = new CountDownLatch(1);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("Location loaded");
snapshotWrapper.snapshot = dataSnapshot;
latch.countDown();
}
@Override
public void onCancelled(DatabaseError dbError) {
System.out.println("Error loading location");
latch.countDown();
}
});
try {
System.out.println("Prelatch");
latch.await();
System.out.println("Returning from latch");
return snapshotWrapper.snapshot.getValue(clazz);
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}
public static DataSnapshot loadSynchronous(Query databaseReference) {
final DataSnapshotWrapper snapshotWrapper = new DataSnapshotWrapper();
final CountDownLatch latch = new CountDownLatch(1);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("Location loaded");
snapshotWrapper.snapshot = dataSnapshot;
latch.countDown();
}
@Override
public void onCancelled(DatabaseError dbError) {
System.out.println("Error loading location");
latch.countDown();
}
});
try {
System.out.println("Prelatch");
latch.await();
System.out.println("Returning from latch");
return snapshotWrapper.snapshot;
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}
public static DataSnapshot loadSynchronous(DatabaseReference databaseReference) {
final DataSnapshotWrapper snapshotWrapper = new DataSnapshotWrapper();
final CountDownLatch latch = new CountDownLatch(1);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("Location loaded");
snapshotWrapper.snapshot = dataSnapshot;
latch.countDown();
}
@Override
public void onCancelled(DatabaseError dbError) {
System.out.println("Error loading location");
latch.countDown();
}
});
try {
System.out.println("Prelatch");
latch.await();
System.out.println("Returning from latch");
return snapshotWrapper.snapshot;
} catch (InterruptedException e) {
e.printStackTrace();
return null;
}
}
private static class DataSnapshotWrapper {
private DataSnapshot snapshot;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment