How to check if a User Data exists in Database or not | Firebase Firestore A complete tutorial || PART 5 ||

Hey, what's up guys, this is a simple small tutorial to check if the user already stored the data in the database or not the database of the user is empty.


Here is the video tutorial for your help





You can check this in your any activity you just need to add this one simple line of code.

firebaseFirestore.collection("Users")
                        .document(curent_user_id)
                        .get()
                        .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                if (task.isSuccessful()) {
                                    if (task.getResult().exists()) {
                                        Toast.makeText(MainActivity.this, "You already have your data Stored in database",
                                                Toast.LENGTH_SHORT).show();
                                    } else {
                                        startActivity(new Intent(MainActivity.this, StoreUserData.class));
                                    }
                                } else {
                                    Toast.makeText(MainActivity.this, "Error: " + task.getException().getMessage(),
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
So in this code we check that in the collection "Users" we have the current user id named document saved or not so if it returns false then the data is not stored so in the oncomplete we check if the task is existed then we show a toast otherwise we send the user to any activity or you can do anything you want...




Its simple to check that.

In the next part, we will fetch those user data to our application and create a profile like a thing.

Here is the video tutorial for your help


Source code Available in GitHub- https://github.com/MonsterTechnoGit/Firebase-Firestore-A-complete-tutorial 

You can get all the series Post using this link-> Click Here


Comments

Post a Comment