Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Sunshine, something that simple could never work.

Sunshine, something that simple could never work.

Scheduled Pinned Locked Moved The Lounge
rubyandroiddesigndata-structures
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cp Coder
    wrote on last edited by
    #1

    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!

    M G 2 Replies Last reply
    0
    • C Cp Coder

      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!

      M Offline
      M Offline
      Marc Clifton
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • C Cp Coder

        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!

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • M Marc Clifton

          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

          C Offline
          C Offline
          Cp Coder
          wrote on last edited by
          #4

          Thanks. I may just try that for fun!

          Get me coffee and no one gets hurt!

          1 Reply Last reply
          0
          • G GuyThiebaut

            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

            C Offline
            C Offline
            Cp Coder
            wrote on last edited by
            #5

            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!

            G 1 Reply Last reply
            0
            • C Cp Coder

              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!

              G Offline
              G Offline
              GuyThiebaut
              wrote on last edited by
              #6

              :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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups