Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android repository pattern for firestore

To have a better separation of concerns I'd like to implement a repository which handles all the firestore interaction.

Currently, my code looks like this:

UserRepository:

public class UserRepository {

    private static final String TAG = "UserRepository";

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference usersRef = db.collection("users");

    public Task<DocumentSnapshot> get(String email) {
        DocumentReference docRef = usersRef.document(email); // Users are referenced by email
        Log.d(TAG, "Checking DocumentReference exists for: " + email);
        return docRef.get();
    }
}

MainActivity

                final UserRepository userRepository = new UserRepository();

                userRepository.get(currentUser.getEmail()).addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        Log.d(TAG, "DocumentSnapshot got");
                        // Do something with user
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d(TAG, "Could not get DocumentReference", e);
                    }
                });

User is a POJO

What I would like to achieve is a Repository which instead of Task returns Task somewhat like this:

public Task<User> get(String email) {
    DocumentReference docRef = usersRef.document(email);
    /* Convert and return task? */
}

Which then could be used like usual, attaching a listener in the MainActivity. I'm afraid I am somewhat lacking skill in concurrency but I feel like this should be possible without having to synchronize. Could you give me a hint on how to achieve this?

Thanks in advance!

like image 555
NiPfi Avatar asked Mar 16 '18 09:03

NiPfi


People also ask

How do I connect my Android to firestore?

In Android Studio, go to “Tools => Firebase” to open the Firebase Assistant. Expand the “Firestore” option located at the bottom and click the link: “Read and write documents with Cloud Firestore” to open the instructions to set up FIrestore.

What is a Subcollection in firestore?

A subcollection is a collection associated with a specific document. Note: You can query across subcollections with the same collection ID by using Collection Group Queries. You can create a subcollection called messages for every room document in your rooms collection: collections_bookmark rooms. class roomA.

What does firebase firestore () do?

Cloud Firestore caches data that your app is actively using, so the app can write, read, listen to, and query data even if the device is offline. When the device comes back online, Cloud Firestore synchronizes any local changes back to Cloud Firestore.


1 Answers

Actually the solution was pretty simple, using a Continuation. I was not sure if task-chaining was what I was actually looking for but it seems like a good solution. The get method now looks like this:

public Task<User> get(String email) {
    DocumentReference docRef = getDocumentReference(email);

    return docRef.get().continueWith(new Continuation<DocumentSnapshot, User>() {
        @Override
        public User then(@NonNull Task<DocumentSnapshot> task) throws Exception {
            return task.getResult().toObject(User.class);
        }
    });
}

Edit: Per request I created a gist containing my implementation: Firestore Repository Gist

like image 96
NiPfi Avatar answered Sep 17 '22 16:09

NiPfi