As Firebase SDK create a demon thread in background to synchronize data. I use the solution suggested as the link to create an AtomicBoolean to wait until the task completed.
java Firebase: delay exit until writes finish
I tried to remove the key in the web admin console. The task become never completed without any timeout exception.
public static void main(String[] args) throws IOException {
InputStream serviceAccount = Appl.class.getClassLoader().getResourceAsStream("testing-fcf995d2ab6f.json");
FirebaseOptions.Builder builder = new FirebaseOptions.Builder();
builder.setCredentials(GoogleCredentials.fromStream(serviceAccount));
builder.setDatabaseUrl("https://golden-attic-93815.firebaseio.com/");
FirebaseOptions firebaseOptions = builder.build();
FirebaseApp.initializeApp(firebaseOptions);
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference ref = firebaseDatabase.getReference("techoffice/database/example");
DatabaseReference usersRef = ref.child("users");
final AtomicBoolean done = new AtomicBoolean(false);
Map<String, User> users = new HashMap<String, User>();
users.put("testerA", new User("Tester A"));
users.put("testerB", new User("Tester B"));
usersRef.setValue(users, new DatabaseReference.CompletionListener(){
public void onComplete(DatabaseError error, DatabaseReference ref) {
if (error != null){
System.err.println("Error " + error.getMessage());
}else {
System.out.println("Completed");
}
done.set(true);
}
});
while(!done.get());
}
I get the same result if I change to setValueAsync.
ApiFuture<Void> apiFuture = usersRef.setValueAsync(users);
apiFuture.get();