<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Patrick Jackson</title>
    <description>The latest articles on Forem by Patrick Jackson (@patjackson52).</description>
    <link>https://forem.com/patjackson52</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F232673%2Fe217249f-8f46-43fc-9133-37ebd6e645e5.png</url>
      <title>Forem: Patrick Jackson</title>
      <link>https://forem.com/patjackson52</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/patjackson52"/>
    <language>en</language>
    <item>
      <title>Coroutine Delay</title>
      <dc:creator>Patrick Jackson</dc:creator>
      <pubDate>Tue, 03 Dec 2019 00:09:28 +0000</pubDate>
      <link>https://forem.com/patjackson52/coroutine-delay-290p</link>
      <guid>https://forem.com/patjackson52/coroutine-delay-290p</guid>
      <description>&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Handler().postDelayed({ doSomething() }, 3000)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { doSomething() }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;A common pattern is to delay execution of some code for a given amount of time. In Android and iOS there are several ways to do, such as the above snippet. How can we pull this into shared Kotlin Multiplatform code?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myCoroutineScope.launch { delay(3000) uiScope.launch { doSomething() } }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the &lt;code&gt;uiScope.launch&lt;/code&gt; runs the &lt;code&gt;doSomething()&lt;/code&gt; function on the main thread. If main thread is not needed &lt;code&gt;uiScope&lt;/code&gt; can be left out.&lt;/p&gt;

&lt;p&gt;There is one trick here for MPP projects. On iOS this will cause a crash at runtime. On iOS you will need to use a custom Dispatcher. Chances are that you are already as a stopgap until multithreaded coroutine support lands.&lt;/p&gt;

&lt;p&gt;Here is a Dispatcher that supports &lt;code&gt;delay()&lt;/code&gt; on iOS. Full source can be found in the &lt;a href="https://github.com/reduxkotlin/ReadingListSampleApp/blob/master/common/src/iosMain/kotlin/org/reduxkotlin/readinglist/common/UI.kt"&gt;ReadingList sample app&lt;/a&gt; from the ReduxKotlin project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import kotlinx.coroutines.*
import platform.darwin.*
import kotlin.coroutines.CoroutineContext

\**
 * Dispatches everything on the main thread. This is needed until
 * multithreaded coroutines are supported by kotlin native.
 * Overrides Delay functions to support delay() on native 
 */
@UseExperimental(InternalCoroutinesApi::class) class UI : CoroutineDispatcher(), Delay {
 override fun dispatch(context: CoroutineContext, block: Runnable) {
   val queue = dispatch_get_main_queue()
   dispatch_async(queue) { block.run() }
 }

   @InternalCoroutinesApi
   override fun scheduleResumeAfterDelay(timeMillis: Long, continuation: CancellableContinuation&amp;lt;Unit&amp;gt;) { 
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeMillis  1_000_000), dispatch_get_main_queue()) {
    try {
      with(continuation) {
        resumeUndispatched(Unit)
      }
    } catch (err: Throwable) { 
      logError("UNCAUGHT", err.message ?: "", err)
      throw err
    }
 }
}

 @InternalCoroutinesApi
 override fun invokeOnTimeout(timeMillis: Long, block: Runnable): DisposableHandle {
   val handle = object : DisposableHandle {
     var disposed = false 
       private set
     override fun dispose() {
       disposed = true
       } 
    }
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeMillis 1_000_000), dispatch_get_main_queue()) {
     try { 
        if (!handle.disposed) {
         block.run()
         }
     } catch (err: Throwable) { 
        logError("UNCAUGHT", err.message ?: "", err)
        throw err
      } 
    }
   return handle
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The post &lt;a href="https://patrickjackson.dev/coroutine-delay/"&gt;Coroutine Delay&lt;/a&gt; appeared first on &lt;a href="https://patrickjackson.dev"&gt;PatrickJackson.dev&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>ios</category>
      <category>kotlinmultiplatform</category>
    </item>
    <item>
      <title>Want to help with ReduxKotlin?  Explore new patterns in Multiplatform Kotlin</title>
      <dc:creator>Patrick Jackson</dc:creator>
      <pubDate>Mon, 02 Dec 2019 02:18:36 +0000</pubDate>
      <link>https://forem.com/patjackson52/want-to-help-with-reduxkotlin-explore-new-patterns-in-multiplatform-ktlin-5gah</link>
      <guid>https://forem.com/patjackson52/want-to-help-with-reduxkotlin-explore-new-patterns-in-multiplatform-ktlin-5gah</guid>
      <description>&lt;p&gt;&lt;a href="https://findcollabs.com/project/reduxkotlin-PwbhxeblEONp4a84YOsG"&gt;https://findcollabs.com/project/reduxkotlin-PwbhxeblEONp4a84YOsG&lt;/a&gt;&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>ios</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
