2

I have recently began migrating from the old Firebase API into the new Google Firebase. I have followed the Getting Started Guide and was able to create an admin access account and generate a JSON file, as well as successfully (I think) initialized the SDK using the example code:

FirebaseOptions options = new FirebaseOptions.Builder()
                .setServiceAccount(new FileInputStream("C:\\FileStructure\\Test Dev Project-59ee6eb5842a.json"))
                .setDatabaseUrl("https://test-dev-project-96fd5.firebaseio.com/")
                .build();

        FirebaseApp.initializeApp(options);

My problem is in pushing any information onto my database. I run the application (In NetBeans) and it runs with no issues.

// Create a database based on the initialized Firebase App
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        // Reference to the root of the database created.
        DatabaseReference ref = database.getReference();
        // Create a child to the root node
        DatabaseReference dataRef = ref.child("Hello World");
        dataRef.setValue("I'm writing data", new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                if (databaseError != null) {
                    System.out.println("Data could not be saved " + databaseError.getMessage());
                } else {
                    System.out.println("Data saved successfully.");
                }
            }
        });

It is my understanding that the code draws a database reference based on the instantiated App, and setValue(), push(), update(), and transaction data pushes data back onto the database.

I have practically copied and pasted data directly from the new API reference page on saving data and the data still does not appear on my console. I can also add that my console does not even confirm that I have correctly or incorrectly pushed the data.

I feel as though I am forgetting something small, and I am not sure if I am creating my references correctly.

I am fairly new to actually posting on Stack Overflow, so please advise if I have left out important data or posted something correctly.

Thanks for your time, guys.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Mark Hall
  • 51
  • 7
  • 2
    The most common cause of this is that your Java program exits before the Firebase client has sent the data to the database. You can easily verify if this is the cause by adding `Thread.sleep(10000)` to the end of your program. – Frank van Puffelen May 26 '16 at 18:14
  • Thank you for the advice. I will try it! I thought that causing a sleep was considered bad coding practice though, will I just have to make an exception in this case? – Mark Hall May 27 '16 at 16:58
  • It looks like you are correct; the Java program does not give time to push the information properly onto the database. My thoughts are, in order to eliminate the need for a Thread.sleep(10000); , I could instead create a JavaFX application that simply advises the user when a push is successful. – Mark Hall May 27 '16 at 18:21
  • 1
    Yup. There are many ways to deal with this, the `Thread.sleep()` was really just for troubleshooting. If you want to do this in a non-interactive Java program, you'd use a semaphore to make it wait until the write is done. – Frank van Puffelen May 27 '16 at 19:47
  • 1
    See http://stackoverflow.com/questions/37098006/firebase-with-java-non-android-retrive-information/37100794#37100794 btw. But I can't mark this as a duplicate, because that answer was neither accepted nor upvoted. Sometimes these rules of Stack Overflow get in the way. :-/ – Frank van Puffelen May 27 '16 at 19:49
  • Hi, Frank, I apologize, I am fairly new here and I don't see any buttons to select it as answered. However, I can post the answer, so I will go ahead and post the answer, and you can mark it as a duplicate. – Mark Hall May 27 '16 at 20:19

1 Answers1

2

A working response from Frank van Puffelen:

The most common cause of this is that your Java program exits before the Firebase client has sent the data to the database. You can easily verify if this is the cause by adding Thread.sleep(10000) to the end of your program

However, it was also brought to my attention that this thread is a duplicate. Original question can be found here. While this question is regarding to the post-transitioned Google owned Firebase, the solution is the same.

Community
  • 1
  • 1
Mark Hall
  • 51
  • 7