0

I have a firebase admin sdk project in java. I have a problem, I cannot get any data or result from my firebase server. here is my project: [[enter image description here](https://i.stack.imgur.com/Gn8XU.png)](https://i.stack.imgur.com/V9ZyJ.png)

I tried at python and program working as normal. My Firebase Database Rules are public.

  • 2
    On Stack Overflow, please [don't show pictures of text and code](https://overflow.tips/write-good-question/formatting-text-code#dont-use-screenshots-of-text). Copy the text into the question itself and format it so that it's easy to read, copy, and search. You can edit the question to correct this using the edit link at the bottom. – Doug Stevenson Jul 10 '23 at 18:29
  • Also, it's unclear from your question what the problem is. I suggest [reviewing this checklist](https://overflow.tips/write-good-question/checklist) for some practical steps to take to improve your question. Questions that just say "[it doesn't work](https://overflow.tips/problematic-questions#why-doesnt-it-work--what-am-i-missing-here)" are not helpful enough to get a good answer. – Doug Stevenson Jul 10 '23 at 18:32
  • Is your code explicitly waiting for the operation to complete? If not, the process may exit before the read has happened. See https://stackoverflow.com/questions/45253891/why-firebase-java-sdk-cant-terminate-after-set – Frank van Puffelen Jul 10 '23 at 18:39
  • @FrankvanPuffelen I cannot communicate with the server in any way. it's same code in Firebase docs. – Fidelio Rainbow Jul 10 '23 at 19:05
  • @FrankvanPuffelen Maybe the program is ending before getting the data. if it is, how to solve this problem. – Fidelio Rainbow Jul 10 '23 at 19:17

1 Answers1

0

Problem solved. I gave the listener some time to get the data. here is the code:

        ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            System.out.println(snapshot);
        }

        @Override
        public void onCancelled(DatabaseError error) {

        }
    });
    TimeUnit.MINUTES.sleep(1);
  • 1
    This is not a good solution. Firstly, you have no guarantee that the write will take 1 second (it could take more, you never know). Only use the callback listeners to know when something changes. – Doug Stevenson Jul 10 '23 at 21:16
  • 1
    Good to hear that this solved the problem. That proves it is the same problem as I mentioned where the JVM exits before the data is read, so I linked your question to the one about that. Using a `sleep` calls is convenient, but (as Doug says) not reliable. In the link I provided I showed how to use a `CountdownLatch` to accomplish the same. – Frank van Puffelen Jul 10 '23 at 22:51