Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thatfiredev opened this issue Jul 5, 2022 · 1 comment
Closed
Assignees

Comments

@thatfiredev
Copy link
Collaborator

thatfiredev commented Jul 5, 2022

I've noticed how this app is using UUID.randomUUID().toString() to generate a document ID and save it on Firestore:

Firebase.firestore
.collection(TASK_COLLECTION)
.document(task.id)
.set(task)
.addOnCompleteListener { onResult(it.exception) }

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
Copy link
Collaborator

Fixed by: #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants