1

In my Firebase app I store the IDs of the users. Then I want to store specific data under the users ID which works as it should. The problem I have is that when the app is reopened, the user specific data is deleted. I suppose it is becuase the user ID is added or overwritten again so it deletes the children nodes. If my spresumption is right, how can I make sure that my user ID is not overwritten? Is there a signUp method that only runs the first time the user gets his ID? I thought to store it in the signInAnonymously() method, but it seems to be called on every start as well.

Here's my sign in code:

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            mAuth = FirebaseAuth.getInstance();
            mDatabase = FirebaseDatabase.getInstance().getReference();

            // active listen to user logged in or not.
            mAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    user = firebaseAuth.getCurrentUser();
                    if (user != null) {
                        // User is signed in
                        Log.d("TAG", "onAuthStateChanged:signed_in:" + user.getUid());
                        userID = user.getUid();
                        mDatabase.child("users").child(userID).setValue(userID);
                        Log.d("uid", userID);
                    } else {
                        // User is signed out
                        Log.d("TAG", "onAuthStateChanged:signed_out");
                    }
                }
            };

            mAuth.signInAnonymously()
                    .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            Log.d("TAG", "OnComplete : " +task.isSuccessful());

                            if (!task.isSuccessful()) {
                                Log.d("TAG", "Authentication Failed : ", task.getException());
                            }
                        }
                    });
   }
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Check if this id exists in the database before doing `setValue` – ronginat Apr 29 '19 at 17:10
  • I thought of that too, but isn't there a better way without an extra query? – user9547680 Apr 29 '19 at 17:12
  • Identify whether it's the [first time that the user opened your app](https://stackoverflow.com/a/13237848/10470378). Only then use `setValue`. Keep in mind that most methods will give you false positive after clearing app's data. – ronginat Apr 29 '19 at 17:18
  • okay, thank you very much so far! – user9547680 Apr 29 '19 at 17:20
  • 1
    For anyone who comes across this question: `boolean isNewUser = task.getResult().getAdditionalUserInfo().isNewUser();` inside the `onComplete()` method of the `signInAnonymously()` method did the trick for me. – user9547680 Apr 29 '19 at 19:19

0 Answers0