Performance overhead of using large number of livedata in a viewmodel
Asked Answered
P

0

12

I need to use multiple recyclerviews. In the below scenario I will require at least 6 recyclerviews. 6 = 1 for horizontal recyclerview + 3 for each fragment in viewpager + 1 for recylerview + 1 for grid layout.

recycler view live data

As the data complexity is huge and each adapter data is dependent on changes from other adapters, I have decided to go with architecture components introduced in the android jetpack. So initially, I will integrate view model and live data. later incorporate room database (existing DB is in SQLite)

As I would be using multiple livedata to monitor adapter data changes. I want clear my doubts on performance aspects of view model and live data.

Performance overhead of using large number of live data :

  1. I will require 6 to 7 live data to observe changes on each adapter data. To get an understanding of performance, assume some 50 ~ 60 live data is to be used.

  2. Is it a best practice or recommended to use live data only with room?.

    can we use it on adapter data or simple primitive types such as booleans, int etc (ex: isLoading: MutableLiveData, inputText: MutableLiveData textField string to monitor changes.)

Say someone may require it to have on a form which may have multiple number edit text, dropdown, multi-select etc with each having a live data attached to it.

  1. How live data works internally does it use some expensive resource to monitor change on a variable
Potvaliant answered 4/12, 2018 at 7:5 Comment(5)
What did you do at the end . did you all those livedata ? or, did you skip the use of it in some places ...Holton
@GilbertoIbarra, I used a single livedata with states(sealed class). That solved my problem. If you are planning to use multiple livedata, that's fine. from a performance perspective, there won't be any impact. However, you will find it challenging to manage all your liveData.Potvaliant
@GilbertoIbarra, If you check the internal implementation of livedata, you will notice that there are no expensive objects being initialized. It has a map of observers and observer themselves are an interface. Livedata is all about calling these observers and handling the scopes effectively.Potvaliant
Yeah I supposed that, but if I make only one change, All the values will be updated, including the adapters, that sounds weird for me, but I supposed that is ok.Holton
@GilbertoIbarra, use different states (the one extending sealed class) to identify which viewholder you want to update and notify only those viewholder. In case you want to update multiple viewholder items, use kotlin flow to emit multiple livedata events.Potvaliant

© 2022 - 2024 — McMap. All rights reserved.