Does anyone know what happens when you call `freez...
# kotlin-native
j
Does anyone know what happens when you call
freeze
on a
CompletableDeferred
? I was playing around with multithreading, created a
CompletableDeferred<SOME_DATA_CLASS>
on the main thread, froze it, passed it to a background thread , then passed it back to the main thread and called
complete(SOME_VALUE)
on it. This seemed to actually work, but it doesn't seem like it should.
here's the code if it's helpful:
Copy code
actual suspend fun parseFrom(url: String): Pair<RssChannel?, Exception?> {
        url.freeze()
        val deferred = CompletableDeferred<Pair<RssChannel?, Exception?>>()
        deferred.freeze()

        val parseInbackground: () -> Unit = {
            iOSRssParser(url) { rssChannel, error ->
                val doOnMain: () -> Unit = {
                    rssChannel?.let {
                        deferred.complete(it to null)
                    }
                    error?.let {
                        deferred.complete(null to it)
                    }
                }
                doOnMain.freeze()
                dispatch_async(dispatch_get_main_queue(), doOnMain)
            }
        }
        parseInbackground.freeze()

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND.convert(), 0.convert()), parseInbackground)

        deferred.await()
        return deferred.getCompleted()
    }
r
If you’re using the native-mt branch, most of the coroutine machinery is freezable. It’s using atomic references internally to handle mutability
j
ah, I see. thanks!!