Last active
May 12, 2020 10:23
-
-
Save deepak140596/66d1a81608b7b2cea8da64e17f219ab4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FirestoreViewModel : ViewModel(){ | |
val TAG = "FIRESTORE_VIEW_MODEL" | |
var firebaseRepository = FirestoreRepository() | |
var savedAddresses : MutableLiveData<List<AddressItem>> = MutableLiveData() | |
// save address to firebase | |
fun saveAddressToFirebase(addressItem: AddressItem){ | |
firebaseRepository.saveAddressItem(addressItem).addOnFailureListener { | |
Log.e(TAG,"Failed to save Address!") | |
} | |
} | |
// get realtime updates from firebase regarding saved addresses | |
fun getSavedAddresses(): LiveData<List<AddressItem>>{ | |
firebaseRepository.getSavedAddress().addSnapshotListener(EventListener<QuerySnapshot> { value, e -> | |
if (e != null) { | |
Log.w(TAG, "Listen failed.", e) | |
savedAddresses.value = null | |
return@EventListener | |
} | |
var savedAddressList : MutableList<AddressItem> = mutableListOf() | |
for (doc in value!!) { | |
var addressItem = doc.toObject(AddressItem::class.java) | |
savedAddressList.add(addressItem) | |
} | |
savedAddresses.value = savedAddressList | |
}) | |
return savedAddresses | |
} | |
// delete an address from firebase | |
fun deleteAddress(addressItem: AddressItem){ | |
firebaseRepository.deleteAddress(addressItem).addOnFailureListener { | |
Log.e(TAG,"Failed to delete Address") | |
} | |
} | |
} |
Hi there! I code in Java and I am having a difficult time in comprehending that FOR loop. I am not able to understand the variables used and if the loop is a normal one or a foreach loop. Thank you!
the loop is foreach one.. It can also be written in Kotlin as
value.forEach { doc ->
var addressItem = doc.toObject(AddressItem::class.java)
savedAddressList.add(addressItem)
}
or in java as
for ( QueryDocumentSnapshot doc : value) {
AddressItem addressItem = doc.toObject(AddressItem.class)
savedAddressList.add(addressItem)
}
What is the variable "value"?
value is QuerySnapshot
for ( QueryDocumentSnapshot doc : value) { AddressItem addressItem = doc.toObject(AddressItem.class) savedAddressList.add(addressItem) }
This shows an error: addSnapshotListener cannot be applied to QuerySnapshots
DO you have the solution?
can you post your whole code? there might be just mis-matching of value types
Also follow this sample code
db.collection("cities")
.whereEqualTo("state", "CA")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}
List<String> cities = new ArrayList<>();
for (QueryDocumentSnapshot doc : value) {
if (doc.get("name") != null) {
cities.add(doc.getString("name"));
}
}
Log.d(TAG, "Current cites in CA: " + cities);
}
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there! I code in Java and I am having a difficult time in comprehending that FOR loop. I am not able to understand the variables used and if the loop is a normal one or a foreach loop. Thank you!