Koya Sivaji
06/15/2020, 9:27 PMI am new to kotlin flows and trying to add Flows to one of my project and facing an issue while converting list of objects of one type to another as shown below. Basically, am not sure which operator to be used for this.
//Repository - method return type is  Flow<List<HistoryEvent>>
fun getHistoryEvents(): Flow<List<HistoryEvent>>
//ViewModel - calling repository method and trying to convert data to List<AccessoryEvent>
val accessoryEvents : LiveData<List<AccessoryEvent>>=  liveData{
                                                      repository.getHistoryEvents()
                                                  // what operator to be used to convert from List<HistoryEvent> to List<AccessoryEvent>
                                                        .collect()
                                                      }
I tried like below
val eventHistory: LiveData<List<AccessoryEventType>> = 
        liveData {
            deviceRepository.getDeviceEventHistoryFor("abcd")
                .map { historyEvents ->
                    emit((historyEvents.map { AccessoryEventType.getEventType(it) }))
                }
                .collect()
        }
after this, all elements in the returned list are same and equals to the last emitted element. Am not sure what I am doing wrongPeter
06/15/2020, 9:31 PMemit in your map ?Peter
06/15/2020, 9:31 PMgetDeviceEventHistoryFor already return a flow ?Koya Sivaji
06/15/2020, 9:32 PMFlow<xx> to Livedata<xx>Peter
06/15/2020, 9:32 PMFlow<List<HistoryEvent>> seems like a smellPeter
06/15/2020, 9:33 PMLiveData is sorryKoya Sivaji
06/15/2020, 9:35 PMList<HistoryEvents> to List<AccessoryEventType>Koya Sivaji
06/15/2020, 9:35 PMgetDeviceEventHistoryFor returns Flow<List<HistoryEvent>>Peter
06/15/2020, 9:36 PMList<HE>.map { convertHEtoAET(it) }Peter
06/15/2020, 9:38 PMemitPeter
06/15/2020, 9:38 PMemit is used when creating a reactive publisher (a new flow)Koya Sivaji
06/15/2020, 9:40 PMLivedata<xx> after doing map without emit