5

I've an API that returns flow which I'm converting to sharedFlow using shareIn(). I've a use case where I want to reset the cached value in the sharedFlow and force the flow to fetch new data. We've a resetReplayCache() however it's only available for MutableSharedFlow.

class User(val id: String) {
   private val _flow = SharedFlow<Data> by lazy {
      Api.observeData(id).map {result-> transformData(result) } . shareIn(scope, timeout, 1)
   }

val flow = _flow


fun forceFetchNewData() {
   _flow.resetReplayCache()// how can I do this?
}
   
}

I'd really appreciate some pointers on either converting the shareFlow to mutableSharedFlow or emptying the sharedFlow cache

General Grievance
  • 4,555
  • 31
  • 31
  • 45
user2498079
  • 2,872
  • 8
  • 32
  • 60
  • What is this line supposed to be? `val flow = flow` Is there some input required to get the data, because in your example it looks like it just fetches one thing, so this shouldn't be a flow at all. – Tenfour04 Jul 18 '22 at 13:02
  • Updated the post. _flow is private and flow is exposed publically – user2498079 Jul 18 '22 at 13:06
  • Is the example oversimplified maybe? `observeData()` has no input parameters, so what would be the meaning be of fetching new data? Why is it a flow in the first place if it isn't automatically emitting fresh data as it becomes available? – Tenfour04 Jul 18 '22 at 13:12
  • yes it's the simplified version. ObserveData is a flow and the subscribers of User class are also expecting flow. Besides this flow is called from several places so I'm using sharedFlow to cache the last emitted value. – user2498079 Jul 18 '22 at 13:19
  • I guess `id` would need to be mutable, right? – Tenfour04 Jul 18 '22 at 13:20
  • Id will remain the same – user2498079 Jul 18 '22 at 13:23
  • That gets back to the same question I was asking. What possible reason is there to refresh a flow if the input parameters have not changed? – Tenfour04 Jul 18 '22 at 13:24
  • 1
    flow will automatically update the values yes. However since the changes are coming from backend. There's a screen in the app where we need to force fetch the latest value. Since sharedFlow keeps last value, it just returns the old value where as I want to force fetch the new data – user2498079 Jul 18 '22 at 13:27

0 Answers0