FirebaseExtended / make-it-so-android

Apache License 2.0
214 stars 58 forks source link

Use Firestore's auto-generated id instead of a random UUID #8

Closed thatfiredev closed 2 years ago

thatfiredev commented 2 years ago

I've noticed how this app is using UUID.randomUUID().toString() to generate a document ID and save it on Firestore: https://github.com/FirebaseExtended/make-it-so-android/blob/40e46da8b587f0466e08a051a41d67c7088083f2/app/src/main/java/com/example/makeitso/model/service/impl/StorageServiceImpl.kt#L69-L73

It would be nice to demonstrate Firestore's auto-generated ids instead:

 Firebase.firestore 
     .collection(TASK_COLLECTION) 
     .add(task) 
     .addOnCompleteListener { onResult(it.exception) } 

And when reading the tasks, we'd manually set the id:

value?.documentChanges?.forEach {
    val wasDocumentDeleted = it.type == REMOVED
    val task = it.document.toObject<Task>().apply { task.id = it.document.id  }
    onDocumentEvent(wasDocumentDeleted, task)
}
marinacoelho commented 2 years ago

Fixed by: https://github.com/FirebaseExtended/make-it-so-android/pull/9