https://kotlinlang.org logo
Title
c

CamilleBC

09/27/2018, 10:22 AM
Hello ! I have a question on Coroutines. I have an Activity (it's for an android program, but the question itself hasn't much to do with android) that tries to connect to a socket using a coroutine. When it fails/succeeds, it calls another function. But the function is possibly executing in another thread, which makes it impossible to call a Toast for example. Is there anyway to have a coroutine call a function that executes in the parent thread ? EDIT: Replaced the pastebin with a public gist with my awful beginner code 🙂 https://gist.github.com/CamilleBC/bfe3b3234d52a0a501656d593c186108
r

robin

09/27/2018, 10:30 AM
If you're using the android extensions you can use the CoroutineContext called
UI
to confine a coroutine to run on that:
launch(Main) {
    // Do UI bound stuff
}
c

CamilleBC

09/27/2018, 10:57 AM
@robin Thanks for the help 🙂 When doing so, it tells me this:
Classifier 'UI' does not have a companion object, and thus must be initialized here
A quick Google search doesn't bring the answer ^^
r

robin

09/27/2018, 10:58 AM
Ah, sorry, it's actually
Main
, not
UI
, my bad!
And you'll probably need
org.jetbrains.kotlinx:kotlinx-coroutines-android
in your project.
👍 1
c

CamilleBC

09/27/2018, 12:09 PM
TThank you so much @robin! I managed to get the
Dispatchers.Main
dispatcher for my coroutineContext. Now my taks is asynchronous, though single threaded (but I don't really care about that). That means everything runs in the main thread! Cheers and thanks for the help! (I am updating the gist at the same time for those who want to see or comment what I have done with robin's help)
r

robin

09/27/2018, 12:18 PM
No problem! Just be careful with doing everything on the Main thread by default, that's how you get ANRs if you're doing too much stuff there. Android will even crash your app deliberately if you try to access the network from your main thread.
c

CamilleBC

09/27/2018, 12:33 PM
Yes, I'm just trying to get a connection on one socket, the app won't launch if it cannot connect to server. That should be all I'm doing on the main thread 🙂
r

robin

09/27/2018, 12:35 PM
Well Android doesn't care how much you do on the network, if you're doing it from the main thread, you're getting killed 😄 So make sure it actually works on newer Android versions. If it works, then it should be fine, maybe that restriction doesn't apply when using sockets.
c

CamilleBC

09/27/2018, 12:57 PM
@robin You are right 😄 Btw, that's not very important but do you know why I need internet permission to connect to the localhost? Seems overkill to me
r

robin

09/27/2018, 1:02 PM
Because the permissions API doesn't check what URI you're trying to access, it's just blocking all network access. Specifically making an exception localhost might also add security concerns, like there might be a way to redirect localhost to any other host.
Out of curiosity, what are you doing that you need to connect to localhost?
s

Sam

09/27/2018, 1:20 PM
c

CamilleBC

09/27/2018, 1:25 PM
@Sam Thanks, I've actually fixed that 😉 @robin Thanks for the explaination. I have a rust root app running that communicates on a local socket 🙂 It is not my choice, I'm building an app that need to communicates with that. Otherwise I'd use Intents