wait until firebase retrieves data

后端 未结 9 1090
不思量自难忘°
不思量自难忘° 2020-11-29 08:33

I want to build a method that returns a child value in FireBase. I tried to do something like this:

public String getMessage(){

           


        
相关标签:
9条回答
  • 2020-11-29 09:05

    As @CMikeB1 commented on another response in the java world when we are dealing with servlets and services rest we use sync calls to perform operations, it would be useful to have both options in sdk firebase admin so that the developer could choose which one to use depending on his case use.

    I made my implementation from waiting to read the sync data using ApiFuture which is the current interface adopted by the sdk of the firebase admin. here's a link!

    public DataSnapshot getMessage() {
        final SettableApiFuture<DataSnapshot> future = SettableApiFuture.create();
        databaseReference.child("MessagesOnLaunch").child("Message").addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                future.set(dataSnapshot);
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
                future.setException(databaseError.toException());
            }
        });
        try {
            return future.get();
        } catch(InterruptedException | ExecutionException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 09:07

    Add this code inside onDataChange() method

    do{
        message = (String) dataSnapshot.getValue();
        if(message.equals(null)) 
            break;
    }while (!message.equals(null));
    
                     
    
    0 讨论(0)
  • 2020-11-29 09:12

    Make an interface

     public interface OnGetDataListener {
        //this is for callbacks
        void onSuccess(DataSnapshot dataSnapshot);
        void onStart();
        void onFailure();
    }
    

    Declare the following function readData()

    public void readData(Firebase ref, final OnGetDataListener listener) {
        listener.onStart();
        ref.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                listener.onSuccess(dataSnapshot);
            }
    
            @Override
            public void onCancelled(FirebaseError firebaseError) {
                listener.onFailure();
            }
        });
    
    }
    

    Call the readData() function as follows

    readData(root.child("MessagesOnLaunch").child("Message"), new OnGetDataListener() {
                    @Override
                    public void onSuccess(DataSnapshot dataSnapshot) {
    
                   //got data from database....now you can use the retrieved data
    
    
                    }
                    @Override
                    public void onStart() {
                        //when starting
                        Log.d("ONSTART", "Started");
                    }
    
                    @Override
                    public void onFailure() {
                        Log.d("onFailure", "Failed");
                    }
                });
    

    readData() can be called inside your getMessage() method or even inside onCreate()

    0 讨论(0)
提交回复
热议问题