Ad
How To Avoid Setting Firestore Document Fields If Already Set
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
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View
Ad