user7838657
user7838657

Reputation:

Firebase reflect changes directly from database?

In my Firebase application I created a fragment that allows users to update their information like namn & email and so far all is going well, however my issue is after the user have updated the information - the changes are not visible untill next relaunch of the application.

How can I reflect the changes directly from the databse without promoting the User to relaunch the app?

I have created a Method called restart(); that will like the name says says restart the application - But still the changes are not being reflected!

/**
     * Update Name Only
     */
    private void updateDisplayNameOnly() {

        showProgress();

        AuthCredential credential = EmailAuthProvider
                .getCredential(FirebaseAuth.getInstance().getCurrentUser().getEmail(), mConfirm.getText().toString());

        FirebaseAuth.getInstance().getCurrentUser().reauthenticate(credential)
                .addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> task) {
                        if (task.isSuccessful()) {
                            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

                            UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
                                    .setDisplayName(mName.getText().toString())
                                    //.setPhotoUri(Uri.parse("https://avatarfiles.alphacoders.com/862/86285.jpg"))
                                    .build();

                            user.updateProfile(profileUpdate);

                            Log.d(TAG, "onComplete: User Profile updated");
                            Toast.makeText(getActivity(), "Name is updated", Toast.LENGTH_SHORT).show();

                            restartApp();

                        } else {
                            Toast.makeText(getActivity(), "Name was not updated", Toast.LENGTH_SHORT).show();
                        }

                        hideProgress();

                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        hideProgress();
                        Toast.makeText(getActivity(), "You have entered wrong password", Toast.LENGTH_SHORT).show();
                    }
                });

    }

Restart Method

public void restartApp() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    startActivity(intent);
    finish();
}

Upvotes: 1

Views: 111

Answers (2)

buggy
buggy

Reputation: 226

Update profile call is asynchronous, and its results are not available immediately, so for some time you'll still observe "obsolete" data.

When you call updateProfile, you get a task as a result. You can subscribe on completion of this task, and if it's completed successfully, then you will be able to get updated data from user instance. E.g.:

final FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
Log.i("MyActivity", "before updateProfile: username=" + currentUser.getDisplayName());

UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
        .setDisplayName("UPDATED_NAME")
        .build();
final Task<Void> task = currentUser.updateProfile(profileUpdate);
Log.i("MyActivity", "after updateProfile: username=" + currentUser.getDisplayName());

task.addOnCompleteListener(new OnCompleteListener<Void>() {
    @Override
    public void onComplete(@NonNull Task<Void> task) {
        Log.i("MyActivity", "onComplete: username=" + currentUser.getDisplayName());
    }
});

And here is an output:

I/MyActivity: before updateProfile: username=old name

I/MyActivity: after updateProfile: username=old name

I/MyActivity: onComplete: username=UPDATED_NAME

Also there is a method user.reload(), which force reload user data from Firebase server. This is useful when your client user cache is obsolete for some reason. It is also an asynchronous method, which gives you Task and you need to subscribe on its completion.

Upvotes: 0

Kim G Pham
Kim G Pham

Reputation: 145

Firebase has a listener onDataChange(), so when you query data from firebase, make sure you implement that, see doc. If you want to reflect the change, implement it in this method (like resetting the text fields). There is no need for a restart method.

Upvotes: 1

Related Questions