How to enable or disable functions in my app depending on fetched data

Gastón Saillén :

I'm working on a profile for an app that requires some data to be fetched from Firebase database and then shown into the user profile.

So far, so good, this requirement is done and all the data is fetched correctly to the user profiles.

But now I have another requirement that put some of my functionality in a spot that I'm trying to figure out how to do it the better way.

I load all the data of the profile with this method (since it's a big method I'm going to shrink it, but the question is not about the code but about logic).

        private void loadProfileData() {
            if (mActivity == null) {
                return;
            }

            mProgressBar.setVisibility(View.VISIBLE);
            mDatabase.child("users").child(mAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {

                        UserModel model = dataSnapshot.getValue(UserModel.class);

    String username = model.getUsername();
  .
  .
  .
     //All data loaded 

    mProgressBar.setVisibility(View.GONE);

...

So, while this data is being fetched I have an overflow menu that has the option to edit the user profile.

enter image description here

Now, that edit profile option is enabled while my data is beign fetched, and I need to pass extras to that Activity with all the user info that is fetched from Firebase in order to load it there and edit it.

Now, I have thought this in two ways

  1. Wait until all async process is done and then enable the edit profile option in the navigation menu
  2. Leave the menu button enabled while the data is being fetched and fetch it again when I'm inside that Activity (that would be reloading all the data instead of passing it as extra which will be more efficient) but at the same time I'm doing two requests for the same data

Is there any better way to implement this functionality?

JakeSteam :

Is it possible to load all user data in the background, perhaps on app start? You can then display the edit button, and assume the current cache of data is correct.

You can still pull the latest copy of the data, and then update the UI accordingly if it's any different. I imagine the data won't change often however, so loading it a few minutes earlier is presumably safe.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=103011&siteId=1