I have the following code: ```suspend fun pickSys...
# ios
p
I have the following code:
Copy code
suspend fun pickSystemImage(): ImageContainer =
    suspendCancellableCoroutine { continuation ->
        MainScope().launch {
            NSLog("pickSystemImage MainScope().launch")
            val imagePicker = UIImagePickerController()
            NSLog("pickSystemImage 1")
            // present and handle
        }
    }
the problem is that initializing
UIImagePickerController
takes huge amount of time - around 6 secs, every time.
Copy code
2021-01-24 21:56:42.439651+0700 Well[3450:1028035] pickSystemImage MainScope().launch
2021-01-24 21:56:48.593539+0700 Well[3450:1028035] pickSystemImage 1
I’ve tried reproducing this in a test project, and there it works fine. I’ve tried switching outer scope, as well as creating scope with main context instead of MainScope, or just without switch to an other scope.. I’m out of the ideas where to look for a solution
l
This is not the right way to use
suspendCancellableCoroutine
at all FYI (you can refere to its doc). You are probably looking for
Dispatchers.Default { }
since it looks like you have a blocking call.
p
I wanna to return a picked image as a result of this suspend function, that’s why
suspendCancellableCoroutine
is used here
MainScope needed here because on the next step I need to present the VC, and it can only be done on main thread
I’ve tried moving it out of the
suspendCancellableCoroutine
Copy code
suspend fun pickSystemImage(): ImageContainer =
    Dispatchers.Main {
        NSLog("pickSystemImage")
        val imagePicker = UIImagePickerController()
        NSLog("imagePicker created")
        //…
    }
Result is the same - 6 seconds to initiate the picker
l
That's probably related to
UIImagePickerController()
itself, or something else in your app keeping the main thread/loop busy. Did you Google
UIImagePickerController
?
p
oh looks like the reason is a connected debugger, without debug it works fine. my mistake was not exploring this direction, thanks!