Hey everyone! I'm looking for some help with setti...
# ktor
c
Hey everyone! I'm looking for some help with setting up my ktor client. I am storing the url in a proto datastore and I'm not sure how to get it out of there and into the
defaultRequest
block. has anyone else did this? How did you go about it?
a
Probably the following solution is kind of bad but you can read an URL synchronously and then use it in the
defaultRequest
block.
c
I had tried that yesterday but it was hanging without end. I thought after a while that it was due to the flow that my repository returns was causing the hang because a flow never ends. I tried using
single()
over
collect{}
but that didn't work either. What I ended up doing was in my runBlocking I just added a small delay
delay(250L)
and that was enough time for the url to be received from my datastore. I still want a better solution than that, my guess is I need to modify my return of flow to something else instead that actually finish which then the solution you gave would work.
maybe a even better way is to not use a default url at all and just get the url when for each request
a
Do you need to get the host only once or it can be updated during an application lifecycle?
c
Theoretically it can be updated during the app lifecycle but right now I am just requiring a restart of the app when the app settings are changed. I know that is not great either but for the time being it going to do. I have a timeline on the project I'm working on so these extra things I can deal with later
so I fully fixed my issue. I created a method with the same name as my val (personal decision) which utilizes
runBlocking{}
and
Flow<T>.first()
to block while the value is retrieve. The reason I did not replace my val with the function is there are places where I need the information as well where I can utilize coroutines properly where I am not initializing components on my app
Copy code
val getTerminalDetails: Flow<TerminalDetails> = cxt.terminalDetails.data
        .catch { e ->
            if (e is IOException) {
                Log.d("Error", e.message.toString())
                emit(TerminalDetails.getDefaultInstance())
            } else {
                throw e
            }
        }

    fun getTerminalDetails(): TerminalDetails = runBlocking {
        cxt.terminalDetails.data.first()
    }