Asynchronous programming in IntelliJ vs Android Studio

huangapple go评论58阅读模式
英文:

Asynchronous programming in IntelliJ vs Android Studio

问题

I like to test programming concepts using these 2 IDEs, however, I cannot help but notice that the Main function in IntelliJ has some differences compared to the OnCreate function in Android.

IntelliJ

fun main(args: Array<String>) {

val flow = flowOf(1, 2, 3)
println("Before launch")

CoroutineScope(Dispatchers.IO).launch {
    println("Launch working")
    flow.collect {
        println("Collect: $it")
    }
}
}

Results

Before launch

This is understandable as I need to use the join on the job object in order to get the coroutine to wait for the Child coroutine to be finished. The code below works fine.

Something like this

fun main(args: Array<String>) = runBlocking {

val flow = flowOf(1, 2, 3)
println("Before launch")

var job = CoroutineScope(Dispatchers.IO).launch {
    println("Launch working")
    flow.collect {
        println("Collect: $it")
    }
}
job.join()}

However, in the Android Studio OnCreate function, I do not need to use the join method to wait for the coroutine to finish.

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
...
lifecycleScope.launch(Dispatchers.Main) {
    requestNews(GENERAL, generalNews, "us")
    requestNews(TECHNOLOGY, TechNews, "us")
    requestNews(HEALTH, healthNews, "us")
    requestNews(SPORTS, SportsNews, "us")
    requestNews(ENTERTAINMENT, EntertainmentNews, "us")
    requestNews(SCIENCE, ScienceNews, "us")
    requestNews(BUSINESS, BusinessNews, "us")
}

}}

The above code in Android Studio's OnCreate function works; all of the requests were initiated. I did not have to use job.join to wait for the coroutine. Is there a reason why there is a difference in the way it works?

IntelliJ requires the need for using the job.join function to execute child coroutines properly while OnCreate doesn't. Am I missing something?

英文:

I like to test programming concepts using these 2 IDEs, however, I cannot help to notice that the Main function in IntelliJ has some differences compared to the OnCreate function in Android.

IntelliJ

fun main(args: Array&lt;String&gt;) {

val flow = flowOf(1, 2, 3)
println(&quot;Before launch&quot;)

CoroutineScope(Dispatchers.IO).launch {
    println(&quot;Launch working&quot;)
    flow.collect {
        println(&quot;Collect: $it&quot;)
    }
}
}

Results

Before launch

This is understandable as I need to use the join on the job object in order to get the coroutine to wait for the Child coroutine to be finished. The code below works fine.

Something like this

fun main(args: Array&lt;String&gt;) = runBlocking {

val flow = flowOf(1, 2, 3)
println(&quot;Before launch&quot;)

var job = CoroutineScope(Dispatchers.IO).launch {
    println(&quot;Launch working&quot;)
    flow.collect {
        println(&quot;Collect: $it&quot;)
    }
}
job.join()}

However in the Android Studio OnCreate function I do not need to use the join method to wait for the coroutine to finish.

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
...
lifecycleScope.launch(Dispatchers.Main) {
    requestNews(GENERAL, generalNews, &quot;us&quot;)
    requestNews(TECHNOLOGY, TechNews, &quot;us&quot;)
    requestNews(HEALTH, healthNews, &quot;us&quot;)
    requestNews(SPORTS, SportsNews, &quot;us&quot;)
    requestNews(ENTERTAINMENT, EntertainmentNews, &quot;us&quot;)
    requestNews(SCIENCE, ScienceNews, &quot;us&quot;)
    requestNews(BUSINESS, BusinessNews, &quot;us&quot;)
}

}}

The above code in Android Studio OnCreate function works,all of the requests were initiated. I did not have to use job.join to wait for the coroutine. Is there a reason why there is a difference in the way it works?

IntelliJ requires the need of using the job.join function to execute child coroutines properly while OnCreate doesn't, am I missing something?

答案1

得分: 1

当我们谈论 Idea 应用程序时,它以 main() 函数结束。但是 Android 应用程序(尤其是 Activity)仍然在运行。

此外,lifecycleScopeActivity 的生命周期绑定,因此当 Activity 被销毁时,您的任务将被取消。因此,onCreate() 不会取消该任务。

英文:

Yep. Just a single concept:
When we are talking about the Idea's app it finishes with the main() function.
But the Android app (especially Activity) will still be running.

Furthermore, lifecycleScope is bound to the Activity lifetime so your job will be canceled when the Activity will be destroyed. Thus onCreate() won't cancel the job.

答案2

得分: 1

你在问题描述中混淆了IDE和目标平台。

  • IntelliJ不是一种应用程序类型,而是一种用于编写各种应用程序的集成开发环境(IDE)。

  • 你可以在IntelliJ IDEA中构建Android应用程序。

  • 你也可以在Android Studio中构建JVM应用程序(尽管它不提供用于创建特定项目的新项目向导)。

所以你的问题实际上是关于JVM应用程序和Android应用程序之间的区别。

JVM应用程序通过执行main函数来工作,一旦main函数返回,它就会终止。因此,如果你启动了一些异步工作而没有等待它完成,那么应用程序将在异步工作完成之前终止。

Android应用程序通过查看应用程序清单中与启动器关联的Activity来工作。然后,它构造关联的Activity子类并设置其上下文和其他依赖项,然后启动其生命周期,其中包括onCreate()函数。Activity不会终止,直到出现某个用户操作,比如退出它或旋转屏幕,或者你调用finish()。应用程序进程(托管Activity的进程)不会终止,直到操作系统决定终止它,或者用户通过Android设置强制关闭它。

这是关于Android工作原理的简化。一个应用程序甚至不一定需要具有任何启动器Activity。它也可以是一个服务。而且启动器Activity不是应用程序可能被启动的唯一方式。

英文:

You're conflating IDEs and target platforms in your question description.

  • IntelliJ is not a type of app. It's an IDE for writing apps of all kinds.

  • You can build Android apps in IntelliJ IDEA.

  • You can build JVM apps in Android Studio (although it doesn't provide a new project wizard for creating a project specifically for that).

So your question is really about the difference between a JVM app and an Android app.

A JVM app works by executing the main function, and it terminates once the main function returns. So, if you start some asynchronous work without waiting for it, then the app will be terminated before the asynchronous work finishes.

An Android app works by looking at the app's manifest for the Activity associated with the launcher. Then it constructs the associated Activity subclass and sets up its Context and other dependencies, and then starts its lifecycle, which includes the onCreate() function. The Activity is not terminated until some user action like backing out of it or rotating the screen, or you calling finish(). The Application process (which was hosting the Activity) is not terminated until the OS decides to, or the user force closes it through the Android settings.

This is a simplification of how Android works. An app doesn't necessarily even need to have any launcher activity. It could be just a service. And the launcher activity is not the only way the application might be started.

huangapple
  • 本文由 发表于 2023年5月11日 03:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定