0

In my Cloud Firestore datababase I have a collection named users. Each user of my app is stored as a document within this collection. My database looks like this:

Firestore-root
   |
   --- users
         |
         --- uid
              |
              --- userName: "Jane"
              |
              --- creationDate: February 28, 2018 at 1:46:00 PM UTC+2

In the creationDate property I want to store the date and time of the user when it first time joins my app. I'm using to populate this property using the answer from this post and it works perfectly fine.

The problem is that every time a user signs in, this property is repopulated and I want it only once, when the user joins my app for the first time.

How can I solve this?

Thanks!

Joan P.
  • 2,368
  • 6
  • 30
  • 63

1 Answers1

0

Check if the user exists, for example:

private String getUser(){
    firestore.collection("user").document("UID")
            .get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {
                        // just fetch data
                    } else {
                        //write data
                    }
                }
            })
        //    .addOnFailureListener()
PeterOne
  • 124
  • 1
  • 4
  • Thanks for your answer but I think you misunderstood my problem. My property is repopulated every time I sign in. If I check for existence and the users exists, what should I do? Not to sign in? – Joan P. Feb 28 '18 at 12:49
  • when it does not exist: create a new user - the "date and time" method. when it exists: use write without date and time ... firestore. ... set(user, SetOptions.merge()) – PeterOne Feb 28 '18 at 13:07