https://kotlinlang.org logo
#ios
Title
# ios
p

Philip Dukhov

01/24/2021, 3:14 PM
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

louiscad

01/24/2021, 8:22 PM
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

Philip Dukhov

01/25/2021, 4:09 AM
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

louiscad

01/25/2021, 9:34 AM
That's probably related to
UIImagePickerController()
itself, or something else in your app keeping the main thread/loop busy. Did you Google
UIImagePickerController
?
p

Philip Dukhov

01/25/2021, 9:51 AM
oh looks like the reason is a connected debugger, without debug it works fine. my mistake was not exploring this direction, thanks!
6 Views