Home > Net >  What does get() makes difference in SharedFlow if I use in Kotlin Android?
What does get() makes difference in SharedFlow if I use in Kotlin Android?

Time:09-08

Is there any difference between this two?

private val _userReviewData = MutableSharedFlow<Resource<ReviewResponse>>()    
val userReviewData get() = _userReviewData.asSharedFlow()

OR

private val _userReviewData = MutableSharedFlow<Resource<ReviewResponse>>()
    val userReviewData = _userReviewData.asSharedFlow()

I don't understand any diff yet!

Also .asSharedFlow() should I defined explicitly or not?

CodePudding user response:

When you use get() i.e. provide a custom getter, then every time you access userReviewData, it will call _userReviewData.asSharedFlow() which is unnecessary. It's better to create a SharedFlow instance once and use it instead of creating multiple such instances.

  • Related