Hello ! I have a question on Coroutines. I have an...
# getting-started
c
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
If you're using the android extensions you can use the CoroutineContext called
UI
to confine a coroutine to run on that:
Copy code
launch(Main) {
    // Do UI bound stuff
}
c
@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
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
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
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
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
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
@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
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
c
@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