Last active
August 17, 2016 14:17
-
-
Save achuinard/e1d8e0e9d71bb2b63884 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <T> T loadSynchronous(final Firebase firebaseLocation, Class<T> clazz) { | |
final DataSnapshotWrapper snapshotWrapper = new DataSnapshotWrapper(); | |
final CountDownLatch latch = new CountDownLatch(1); | |
firebaseLocation.addListenerForSingleValueEvent(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
System.out.println("Location loaded"); | |
snapshotWrapper.snapshot = dataSnapshot; | |
latch.countDown(); | |
} | |
@Override | |
public void onCancelled(FirebaseError firebaseError) { | |
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; | |
} | |
} | |
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