Home > Blockchain >  I need to wait for a request response in the main thread Android Studio
I need to wait for a request response in the main thread Android Studio

Time:12-05

I need to make a request to an endpoint in the app startup, i really need to wait for the response to let the app continue running, so i need to make a sync request

how i can do it?

CodePudding user response:

Any long-running sync request on the main thread will definitely trigger Application Not Responding (ANR) error.

It's highly recommended to move each IO (network, storage) operation to a worker thread. To achieve this you can use a stack of Kotlin Coroutines, Retrofit and Moshi.

To prevent the app from running until the network response, you should implement splash screen. Configure its lifetime as explained here.

CodePudding user response:

i really need to wait for the response to let the app continue running, so i need to make a sync request

No, you don't. You need to provide some temporary UI that does nothing (like a loading spinner) while the async request completes.

Read up on coroutines for background work.

Making a "sync" request is just going to hang your app.

  • Related