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

后端 未结 2 905
孤街浪徒
孤街浪徒 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<DocumentSnapshot>() {
            @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.

    0 讨论(0)
  • 2020-11-21 05:38

    Use LiveData as return type and observe the changes of it's value to execute desired operation.

    private MutableLiveData<UserAccountSettings> userSettingsMutableLiveData = new MutableLiveData<>();
    
    public MutableLiveData<UserAccountSettings> getUserSettings(DocumentSnapshot documentSnapshot){
    
        DocumentReference mSettings = mFirebaseFirestore.collection("user_account_settings").document(userID);
        mSettings.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                UserAccountSettings settings = documentSnapshot.toObject(UserAccountSettings.class);
                settings.setDisplay_name(documentSnapshot.getString("display_name"));
                settings.setUsername(documentSnapshot.getString("username"));
                settings.setWebsite(documentSnapshot.getString("website"));
                settings.setProfile_photo(documentSnapshot.getString("profile_photo"));
                settings.setPosts(documentSnapshot.getLong("posts"));
                settings.setFollowers(documentSnapshot.getLong("followers"));
                settings.setFollowing(documentSnapshot.getLong("following"));
    
                userSettingsMutableLiveData.setValue(settings);
            }
        });
    
        return userSettingsMutableLiveData;
    }
    

    Then from your Activity/Fragment observe the LiveData and inside onChanged do your desired operation.

    getUserSettings().observe(this, new Observer<UserAccountSettings>() {
        @Override
        public void onChanged(UserAccountSettings userAccountSettings) {
            //here, do whatever you want on `userAccountSettings`
        }
    });
    
    0 讨论(0)
提交回复
热议问题