What’s the best way to use `IO.parMapN` such that ...
# arrow
k
What’s the best way to use
IO.parMapN
such that I jump back into a differnet coroutinecontext after the parallel operations have completed?
Copy code
IO.parMapN(IOThread, someIO, anotherIO) { a, b ->
  continueOn(MainThread)
  // do stuff
}
Does not appear I can call
continueOn
inside
parMapN
. I tried
IO.parMapN(…).continueOn(MainThread)
but that doesn’t seem to actually switch me correctly. I actually have
Copy code
ReaderT { 
  ... IO.parMapN(IOThread, ...).continueOn(MainThread)
}.run(context).fix().unsafeRunAsync {
  it.fold({}, { myLiveData.value = it })
}
and Android throws, telling me I can’t call
setValue
from a background thread. So the
continueOn
doesn’t seem to change the coroutine context back to the main thread that way.
Because the first IO in parMapN is just a IO-wrapped suspend function call, I have currently re-written it so that now I have
Copy code
IO.parMapN(MainThread, FormerlyIOWrappedSuspend, ...) { ... }

fun FormerlyIOWrappedSuspend() = IO.fx {
  continueOn(IOThread)
  val rv = effect { suspendFn() }.bind()
  continueOn(MainThread)
  rv
}