0

I have created a small application, that should get data from Firebase realtime database. The problem is, I will delete all data after get the information from the Firebase. I have searched for the solution, but all the solutions I found did not work for me. I hope you can help me with my problem.

class Example {
    
    public static Object data1;

    public static void main(String[] args) throws IOException, InterruptedException {
        FileInputStream serviceAccount = new FileInputStream("./google-services.json");
        FirebaseOptions options = FirebaseOptions.builder()
                .setCredentials(GoogleCredentials.fromStream(serviceAccount))
                .setDatabaseUrl("https://example.firebaseio.com")
                .build();
      
        CountDownLatch latch = new CountDownLatch(2);
        Query reference1 = FirebaseDatabase.getInstance().getReference("/\"UserName\"/"data1");
      
        ref1.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot datasnapshot) {
                data1 = datasnapshot.getValue();
                System.out.println(data1); 
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            };
        });
        
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("/\"UserName\"");
        databaseReference.child("/\"data1\"");
        databaseReference.removeValue();
        System.out.println("Successfully deleted user");
        }
  • What doesn't work about the code you shared? Specifically: when you step through this code in a debugger, which line doesn't do what you expect it to do? – Frank van Puffelen Feb 18 '21 at 15:55
  • sorry i do not have seen your comment, what i post here that work fine, but i try to find a solution, to delete the data in the firebase after i have retrieve the data from Firebase. I have edit the code, the last part does not work. – den4coding Feb 19 '21 at 12:05
  • This string escaping looks *highly* unusual: `.getReference("/\"UserName\"/"data1")`. Are you sure this is needed? Can you add a sample of your JSON (as text, no screenshots please)? You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Feb 19 '21 at 15:30
  • I use Intellij, so it works for me with following method: `public static void removeAllExample() { FirebaseDatabase.getInstance().getReference("User Name").removeValueAsync(); System.out.println("User successfully deleted!!"); }` The Problem is, it work only with this command: "removeValueAsync", but i search for another way to do the same thing. – den4coding Mar 09 '21 at 12:59
  • So i will explain more what i need, i have some tasks to retrieve Data from Firebase and writte it to other space, but because it all runs asynchron, so tasks that should run after each other run asynchron and i get the result, that i do not want, it is possible to run tasks synchronoius? – den4coding Mar 09 '21 at 15:24
  • No, that is not possible with the Firebase SDK. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 and https://stackoverflow.com/questions/66072471/what-is-the-difference-between-get-and-addlistenerforsinglevalueevent – Frank van Puffelen Mar 09 '21 at 16:06
  • it is possible in another way? – den4coding Mar 10 '21 at 14:00

1 Answers1

0

I use these:

//1.
//Remove value from child   
mFirebaseDatabase!!.child(userId!!).removeValue()  
db.makeText(applicationContext, "Successfully deleted user", db.LENGTH_SHORT).show()  
  
//2.
// clear information  
user_val.setText("")  
maxdev
  • 13
  • 5
  • thank you for the answer, this example "child(userID!!).removeValue()", will it only remove the data from particilar user or from all users, because i will delete all data from realtime database after read. – den4coding Feb 18 '21 at 13:10