Sunshine, something that simple could never work.
-
I am working on a Kotlin app that needs to load a large file at startup. Because it can take up to 20 seconds it must be done in a separate thread so it won't block the UI thread. When it is loaded I need to send a signal to the main UI thread, so that the main thread will know the file is ready for further processing. Being a Kotlin greenhorn, I battled for hours to send a signal from the background thread to the main thread, with no luck. Then I remembered reading something about a built-in Kotlin method :
runOnUiThread()
. "Surely it cannot be that simple", I thought. But in desperation I tried adding this statement to the end of the background thread:
runOnUiThread { someMethodOnUiThread() }
And voila! it worked. Apparently this little gem inserts the requested method into the execution queue of the UI thread. Sometimes we look for complicated solutions when a perfect simple solution is right under our noses. By the way: Please don't tell me I should have used a Kotlin coroutine. I tried that, but when you use a file reading class like ObjectInputStream the coroutine starts blocking the UI thread.
Get me coffee and no one gets hurt!
-
I am working on a Kotlin app that needs to load a large file at startup. Because it can take up to 20 seconds it must be done in a separate thread so it won't block the UI thread. When it is loaded I need to send a signal to the main UI thread, so that the main thread will know the file is ready for further processing. Being a Kotlin greenhorn, I battled for hours to send a signal from the background thread to the main thread, with no luck. Then I remembered reading something about a built-in Kotlin method :
runOnUiThread()
. "Surely it cannot be that simple", I thought. But in desperation I tried adding this statement to the end of the background thread:
runOnUiThread { someMethodOnUiThread() }
And voila! it worked. Apparently this little gem inserts the requested method into the execution queue of the UI thread. Sometimes we look for complicated solutions when a perfect simple solution is right under our noses. By the way: Please don't tell me I should have used a Kotlin coroutine. I tried that, but when you use a file reading class like ObjectInputStream the coroutine starts blocking the UI thread.
Get me coffee and no one gets hurt!
I thought that's what
postMessage
does - posts a message onto the UI thread. Window.postMessage() - Web APIs | MDN[^] But then again, I don't write apps in Kotlin so I needed something more native, using the Worker class Using Web Workers - Web APIs | MDN[^] and various machinations to deal with the fact that I didn't want external files for the source for the worker. :rolleyes:Latest Articles:
DivWindow: Size, drag, minimize, and maximize floating windows with layout persistence -
I am working on a Kotlin app that needs to load a large file at startup. Because it can take up to 20 seconds it must be done in a separate thread so it won't block the UI thread. When it is loaded I need to send a signal to the main UI thread, so that the main thread will know the file is ready for further processing. Being a Kotlin greenhorn, I battled for hours to send a signal from the background thread to the main thread, with no luck. Then I remembered reading something about a built-in Kotlin method :
runOnUiThread()
. "Surely it cannot be that simple", I thought. But in desperation I tried adding this statement to the end of the background thread:
runOnUiThread { someMethodOnUiThread() }
And voila! it worked. Apparently this little gem inserts the requested method into the execution queue of the UI thread. Sometimes we look for complicated solutions when a perfect simple solution is right under our noses. By the way: Please don't tell me I should have used a Kotlin coroutine. I tried that, but when you use a file reading class like ObjectInputStream the coroutine starts blocking the UI thread.
Get me coffee and no one gets hurt!
Quote:
coroutine starts blocking the UI thread
I believe that you can set the dispatcher context of the coroutine thread so that it runs on a thread other than the UI thread. Dispatchers in Kotlin Coroutines - GeeksforGeeks[^] That said if your solution works then that's fine as far as I am concerned(I am from the school of software needs to work rather than it needs to be beautifully architected but not work) - Kotlin still seems to have a few quirks with coroutines.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
I thought that's what
postMessage
does - posts a message onto the UI thread. Window.postMessage() - Web APIs | MDN[^] But then again, I don't write apps in Kotlin so I needed something more native, using the Worker class Using Web Workers - Web APIs | MDN[^] and various machinations to deal with the fact that I didn't want external files for the source for the worker. :rolleyes:Latest Articles:
DivWindow: Size, drag, minimize, and maximize floating windows with layout persistence -
Quote:
coroutine starts blocking the UI thread
I believe that you can set the dispatcher context of the coroutine thread so that it runs on a thread other than the UI thread. Dispatchers in Kotlin Coroutines - GeeksforGeeks[^] That said if your solution works then that's fine as far as I am concerned(I am from the school of software needs to work rather than it needs to be beautifully architected but not work) - Kotlin still seems to have a few quirks with coroutines.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
My experience with coroutines is that they run in non-blocking mode, until you start doing disk io. Then they block the UI thread. I have also seen a number of posts confirming this behavior. I think it was on Stackoverflow.
Get me coffee and no one gets hurt!
-
My experience with coroutines is that they run in non-blocking mode, until you start doing disk io. Then they block the UI thread. I have also seen a number of posts confirming this behavior. I think it was on Stackoverflow.
Get me coffee and no one gets hurt!
:thumbsup:go with what works. I would have thought that disk IO would occupy the IO thread but as you hint coroutines can be a bit weird.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens