google-developer-training / basic-android-kotlin-compose-training-inventory-app

Apache License 2.0
77 stars 84 forks source link

Read and update data using Room #12

Closed damien7792 closed 1 year ago

damien7792 commented 1 year ago

URL of codelab: https://developer.android.com/codelabs/basic-android-kotlin-compose-update-data-room?continue=https%3A%2F%2Fdeveloper.android.com%2Fcourses%2Fpathways%2Fandroid-basics-compose-unit-6-pathway-2%23codelab-https%3A%2F%2Fdeveloper.android.com%2Fcodelabs%2Fbasic-android-kotlin-compose-update-data-room#7

In which task and step of the codelab can this issue be found? In the CodeLab "Read and update data with Room", step 8 "Implement sell item" Then, in point 4: "Retrieve the entities with itemDao.getAllItems(). Compare them to the updated entity and assert."

Describe the problem It's not really a problem, but I'm puzzled by something: the ".first()" operator that seems to return a list. I come from the .Net world, and in C# when you end a series of instructions with ".First()", you expect to get back a single item, not a list. Here, it seems that by ending with ".first()", "allItems" contains a list of items: we can access allItems[0] and allItems[1]

Can you clarify how the "first()" operator works in that case?

Thank you!

cjparent commented 1 year ago

In this case, itemDao.getAllItems() is returning a Flow (of type Flow<List>) as opposed to a list. When used on a Flow, the first() operator "returns the first element emitted by the flow and then cancels flow's collection". As such, since the itemDao.getAllItems returns type Flow<List>, the first() operator will return the first List emitted by the flow.

android-dev-lxl commented 1 year ago

@cjparent Thank you for the reply.

@damien7792 Hope that answers your question. Please let us know, if you have any other questions.

damien7792 commented 1 year ago

@cjparent Sorry for the late reply. Yes thanks, that makes sense!