Ad

How To Avoid Setting Firestore Document Fields If Already Set

- 1 answer

Firebase document field are getting reset each time the activity starts, I need them to persist.

Creation of the collection and document is being done in onResume with "setOptions.merge()", with documents values being set to null for String and emptyList for array.

    @Override
    protected void onResume() {
        super.onResume();

        //Creating the document if it does not already exist.

        Map<String, Object> read_book = new HashMap<>();
        read_book.put("read_pages", Collections.emptyList());
        read_book.put("total_pages", null);

        fireStoreDB.collection("user_" + user.getId).document("book_" + book.getId())
                .set(read_book, SetOptions.merge())
                .addOnSuccessListener(aVoid ->
                        Log.d("Activity", "FirestoreDB => created empty read_pages and total_pages"))
                .addOnFailureListener(e ->
                        Log.d("Activity", "Firestore failed to create empty read_pages and total_pages" + e));
    }

private void pageFlip(String currentPage, String totalPages){

        // Add read page to array (no duplicates)

        fireStoreDB.collection("user_" + user.getId).document("book_" + book.getId())
                .update("read_pages", FieldValue.arrayUnion(currentPage))
                .addOnSuccessListener(aVoid ->
                        Log.d("Activity", "FirestoreDB => updated read_pages " + currentPage))
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("Activity", "FirestoreDB => failed to update read_pages" + e);
                    }
                });

        // Set total book pages number

        fireStoreDB.collection("user_" + user.getId).document("book_" + book.getId())
                .update("total_pages", totalPages)
                .addOnSuccessListener(aVoid ->
                        Log.d("Activity", "FirestoreDB => updated total_pages " + totalPages))
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("Activity", "FirestoreDB => failed to update total_pages" + e);
                    }
                });
}

With the above code the field are being reset to null and empty list every time onResume is called.

I need total_pages value and read_pages array to persist across Activity restarts with the possibility to add new values to read_pages array with FieldValue.arrayUnion.

What is an efficient way to implement my use-case?

Ad

Answer

You can use set method with SetOptions.merge() directly no need to initialize with null value

private void pageFlip(String currentPage, String totalPages){

  Map<String, Object> data = new HashMap<>();
        data.put("total_pages", totalPages);
        data.put("read_pages", FieldValue.arrayUnion(currentPage));


        // Add read page to array (no duplicates)

        fireStoreDB.collection("user_" + user.getId).document("book_" + book.getId())
                .set(data, SetOptions.merge())
                .addOnSuccessListener(aVoid ->
                        Log.d("Activity", "FirestoreDB => updated read_pages " + currentPage))
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("Activity", "FirestoreDB => failed to update read_pages" + e);
                    }
                });


}
Ad
source: stackoverflow.com
Ad