How to return a DocumentSnapShot as a result of a method?

后端 未结 2 902
孤街浪徒
孤街浪徒 2020-11-21 04:36

A custom object that takes a parameter of (DocumentSnapShot documentsnapShot). also is an inner object from Firebase that retrieves a snapshot and set the values to my custo

2条回答
  •  遇见更好的自我
    2020-11-21 05:29

    You cannot return something now that hasn't been loaded yet. Firestore loads data asynchronously, since it may take some time for this. Depending on your connection speed and the state, it may take from a few hundred milliseconds to a few seconds before that data is available. If you want to pass settings object to another method, just call that method inside onSuccess() method and pass that object as an argument. So a quick fix would be this:

    @Override
    public void onSuccess(DocumentSnapshot documentSnapshot) {
        UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
        yourMethod(settings);
    }
    

    One more thing to mention is that you don't need to set the those values to object that already have them. You are already getting the data from the database as an object.

    So remember, onSuccess() method has an asynchronous behaviour, which means that is called even before you are getting the data from your database. If you want to use the settings object outside that method, you need to create your own callback. To achieve this, first you need to create an interface like this:

    public interface MyCallback {
        void onCallback(UserAccountSettings settings);
    }
    

    Then you need to create a method that is actually getting the data from the database. This method should look like this:

    public void readData(MyCallback myCallback) {
        DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
        mSettings.get().addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
                myCallback.onCallback(settings);
    
            }
        });
    }
    

    In the end just simply call readData() method and pass an instance of the MyCallback interface as an argument wherever you need it like this:

    readData(new MyCallback() {
        @Override
        public void onCallback(UserAccountSettings settings) {
            Log.d("TAG", settings.getDisplay_name());
        }
    });
    

    This is the only way in which you can use that object of UserAccountSettings class outside onSuccess() method. For more informations, you can take also a look at this video.

提交回复
热议问题