Hi, how can I phrase this code so I don't have thr...
# getting-started
n
Hi, how can I phrase this code so I don't have three identical
ResultsChips
for each headline? I understand why I am getting this result (because of the loops), but I have no idea how to resolve it. 🧵
Copy code
val dateTimeFormatter: DateTimeFormatter =
                    DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss O yyyy")

                items(newsFeeds?.sortedByDescending {item->
                    LocalDateTime.parse(item.feedItem.pubDate, dateTimeFormatter)
                }?.filter { it.feedTitle == title } ?: emptyList()) { item ->
                    ResultsChip(item.id, item.feedItem.title, navController)
                }
j
I think we're missing a bunch of context here, I'm not sure how to help right now. What loops are you talking about? What does the list that you pass to
items()
look like (once sorted and filtered)? Also, what's the difference between
item.feedTitle
and
item.feedItem.title
? If they are the same, what would you expect to look different in the
ResultsChip
?
Also, is that a JetPack Compose list? (because that's also unclear to me at first glance, I'm not familiar with it, and this is not the #compose-android channel, so it's not a default context)
n
Hi, well by loops I mean the fact that I have
item->
twice. Yes, it's a Compose list.
👌 1
Ok, I'll ask them!
😊
j
Oh I'm not saying your post doesn't belong here, I'm just saying the channel gives context. But in #getting-started you should assume we don't have any context 🙂
👍 1
n
I asked here because I am trying to order my list according to its publication date.
Until I add the sorting code, all works fine.
The original code is this:
Copy code
items(newsFeeds?.filter { it.feedTitle == title } ?: emptyList()) { item ->
                    ResultsChip(item.id, item.feedItem.title, navController)
                }
Shall I ask the other channel?
j
Not necessarily, as the problem might not come from the Compose list itself. I think you should extract the list to a variable rather than putting the whole expression in the
items()
parameter. It will be a bit clearer and will also allow to print the list for debugging, or put a breakpoint more easily. What does the list look like before and after sorting? What about after filtering?
By the way, this is unrelated, but note that if you filter the list, there is no real point in sorting the whole list, so it is better to filter first, and then sort.
n
The list is fetched online and stored into a room database. It is then observed from there. It has a number of attributes like: title, date, link...
What I want is for the titles to appear in the order that they are published, latest first.
j
Sorry I wasn't clear, I meant what is the list exactly? What values do you see for the input list when you witness the behaviour of
ResultsChip
you're describing? (what titles, what dates, what's the size of the list, before filtering, after filtering, this kind of things). Did you look at this when debugging?
Probably you see duplicates because you have duplicates in the input data.
Until I add the sorting code, all works fine
Does it work fine again when you remove the sorting code and go back to just filtering? Are you sure the dupicates are not just farther apart, and you just don't notice them?
n
No, there are no duplicates, I am sure of that. This is what my room database looks like.
At this stage all I need is to show the headline/title on a chip.
which is working well, but I wanted them ordered
j
This is what my room database looks like
That is just a tiny extract, though. Or does the whole table only contain items 55-64?
n
yeah
I'll try a better answer
The lists vary in size depending on which rss feed is being downloaded. Some of them only have a few headlines and others have 200, like the Guardian.
j
I mean,
sortingByDescending
cannot add elements to the list, so if you see duplicates eventually, they must have been there originally. Sorting does bring duplicates next to each other, making the duplication more obvious in long lists. So I believe if you see duplicate chips, and you want to be sure there are no duplicates in DB, you should run a query with the duplicate title from the chips and see how many results you find in DB
n
But then why would there be three identical chips for every single headline on every newspaper?
j
Because maybe you did 3 tests, and inserted 3 times the same RSS feeds in your DB without unique constraints?
n
That makes sense
I will analyse the database
👍 1
thanks!
j
No problem
n
Yes, you were spot on!
j
Nice, I'm glad you found the issue! ☺️
👍 1
Now if you want to prevent duplication you should probably add some logic during insertion to filter out duplicates (for instance based on the last known post date for a given feed), and maybe add a unicity constraint in the DB on some set of columns to fail in case of duplicate insertion so the problem is prevented at the root
n
Yes, sounds like a good plan of action.
x
Despite this not being a Compose-related Slack channel, I’ve seen your example and I do wanted to point out two concerns: • You should perform collection operations inside
remember
blocks for performance reasons • If you’re using DB models in Compose, those may all be unstable and lead to a whole lot of recompositions on scrolling If you want more information on this, we can continue this topic by DM
n
Thank you so much. Will get in touch by DM after the weekend. 🙂
❤️ 1