Itay Braha
Itay Braha

Reputation: 536

How to use Firebase Cloud Firestore references with clean code?

So, I'm using Firebase Cloud and I have three references that I go through until I get my data.

this issue made me create 3 anonymous nested classes- which looks horrible, hard to follow and probably hard code to maintain.

I've added that code, I hope for your advice - how should I clean it up?

db.collection("users").document(mAuth.getUid())
                .get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if (task.isSuccessful()) {
                            DocumentSnapshot document = task.getResult();
                            if (document.exists()) {
                                ((DocumentReference)document.get("Users_Project")).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                    @Override
                                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                        if (task.isSuccessful()) {
                                            DocumentSnapshot document = task.getResult();
                                            if (document.exists()) {
                                                ((DocumentReference) document.get("Project_Schedule")).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                                    @Override
                                                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                                        if (task.isSuccessful()) {
                                                            DocumentSnapshot document = task.getResult();
                                                            if (document.exists()) {
                                                                //we got the data!!!!
                                                                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                                                            } else {
                                                                Log.d(TAG, "No such document as projectschedule ref");
                                                            }
                                                        } else {
                                                            Log.d(TAG, "get failed with ", task.getException());
                                                        }
                                                    }
                                                });
                                            } else {
                                                Log.d(TAG, "No such document as projectschedule");
                                            }
                                        } else {
                                            Log.d(TAG, "get failed with ", task.getException());
                                        }
                                    }
                                });
                            } else {
                                Log.d(TAG, "No such document as userproj");
                            }
                        } else {
                            Log.d(TAG, "get failed with ", task.getException());
                        }
                    }
                });

Thanks in advance!

Upvotes: 0

Views: 383

Answers (1)

Mitesh Machhoya
Mitesh Machhoya

Reputation: 434

you can try something like this :

db.collection("users").document(mAuth.getUid())
        .get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                callback1(document);
            } else {
                Log.d(TAG, "No such document as userproj");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

    private void callback1(DocumentSnapshot document)
    {
        ((DocumentReference)document.get("Users_Project")).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    callback2();
                } else {
                    Log.d(TAG, "get failed with ", task.getException());
                }
            }
        });
    }

    private void callback2(DocumentSnapshot document)
    {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            ((DocumentReference) document.get("Project_Schedule")).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        callback3();
                    } else {
                        Log.d(TAG, "get failed with ", task.getException());
                    }
                }
            });
        } else {
            Log.d(TAG, "No such document as projectschedule");
        }
    }

    private void callback3(DocumentSnapshot document)
    {
        DocumentSnapshot document = task.getResult();
        if (document.exists()) {
            //we got the data!!!!
            Log.d(TAG, "DocumentSnapshot data: " + document.getData());
        } else {
            Log.d(TAG, "No such document as projectschedule ref");
        }
    }

Upvotes: 2

Related Questions