2

Single type results from Room in Flow streams have an option to filter out unchanged results distinctuntilchanged, this works perfectly for single type results (ex. Flow<MyObject>, Flow<String>, Flow<Int>, etc.)

However, this is no longer the case when the returning result is a list of objects, ex. Flow<List<MyObject>>, at least in my case.

In this example, my object type MyObject is as follows

class MyObject : BaseEntity() {
    ...
}

Where BaseEntity is a master Room Entity object with basic properties that all objects must share in my project.

I understand that PagingDataAdapter exists, but with it comes a huge boilerplate which is very impractical for small/single use lists, especially when these lists aren't meant to be displayed in the UI, so for cases where the list is not shown to the user this is not a solution.

How can I make the returned list results be distinct so it won't trigger changes if the list has none? Do I have to use a different object type? Do I have to implement a specific logic somewhere?

Shadow
  • 4,168
  • 5
  • 41
  • 72
  • Either implement `hashcode()` and `equals()` on your entitiy, or use a custom comparator : `fun Flow.distinctUntilChanged(areEquivalent: (T, T) -> Boolean): Flow` – Mark Nov 15 '21 at 19:04
  • @MarkKeen I don't understand how the second option would work for my situation – Shadow Nov 16 '21 at 00:28
  • "How can I make the returned list results be distinct so it won't trigger changes if the list has none" - `flowList.filter{ it.isNotEmpty() }.distinctUntilChanged { ... }`? – Mark Nov 16 '21 at 01:44
  • @MarkKeen that only filters out empty lists, my question has been clearly addressing to filter out when a list contains no change "won't trigger changes if the list has none" as in "has no changes". – Shadow Nov 16 '21 at 14:46
  • I would look at the documentation for list equals() method - as I stated override equals() and hashcode() in you type object .. that's is it .. if changes are triggered when there are "no changes" then either you have not overridden these methods, or the implementation is faulty, if you don't want to override these methods, then use a custom comparator, also mentioned above. I'm not sure how this is difficult to follow or even bother trying before saying "it won't work". You could even use diffutil in the custom comparator. – Mark Nov 16 '21 at 15:25
  • did you get an answer? – Cpp crusaders Jan 07 '23 at 12:49
  • @Cppcrusaders sort of, the workaround that I managed to implement was to use data classes with all val properties, period. Kotlin does automatic diff on those data classes and it works for my purpose. It was the only way I found to this day. – Shadow Jan 09 '23 at 02:19
  • Hi @shadow, thanks for the quick reply... say you have a room db with 5 records... say your activity is listening on this room db.. Now if one record is added to the the room db... how many records with your activity get... 1 record or all 6 records? – Cpp crusaders Jan 09 '23 at 08:00
  • @Cppcrusaders If you're using Flow then you will get only the changed record in this fashion because it can doe its own diff if we use data class models. – Shadow Jan 09 '23 at 14:43

0 Answers0