{"id":4507,"date":"2021-05-19T17:24:52","date_gmt":"2021-05-19T12:54:52","guid":{"rendered":"http:\/\/milmit.net\/?p=4507"},"modified":"2023-06-28T15:38:22","modified_gmt":"2023-06-28T12:08:22","slug":"edu_4507","status":"publish","type":"post","link":"https:\/\/milmit.net\/en\/edu_4507\/","title":{"rendered":"What are Coroutines?"},"content":{"rendered":"<h3>What are Coroutines?<\/h3>\n<p>A series of Android programming tutorials to teach Coroutines in Android. Coroutines were added to Android with Kathleen. Join us to learn more about this code.<\/p>\n<pre class=\"lang: decode:true\">Coroutines = Co + Routines<\/pre>\n<p>Here co means cooperation and routines means function. This means that the functions work in relation to each other.<\/p>\n<p>Well, let&#8217;s go to the example. Below is a simple example with two functions<\/p>\n<p>&nbsp;<\/p>\n<p>functionA<\/p>\n<pre class=\"lang:default decode:true\">fun functionA(case: Int) {\r\n    when (case) {\r\n        1 -&gt; {\r\n            taskA1()\r\n            functionB(1)\r\n        }\r\n        2 -&gt; {\r\n            taskA2()\r\n            functionB(2)\r\n        }\r\n        3 -&gt; {\r\n            taskA3()\r\n            functionB(3)\r\n        }\r\n        4 -&gt; {\r\n            taskA4()\r\n            functionB(4)\r\n        }\r\n    }\r\n}<\/pre>\n<p>functionB<\/p>\n<pre class=\"lang:default decode:true\">fun functionB(case: Int) {\r\n    when (case) {\r\n        1 -&gt; {\r\n            taskB1()\r\n            functionA(2)\r\n        }\r\n        2 -&gt; {\r\n            taskB2()\r\n            functionA(3)\r\n        }\r\n        3 -&gt; {\r\n            taskB3()\r\n            functionA(4)\r\n        }\r\n        4 -&gt; {\r\n            taskB4()\r\n        }\r\n    }\r\n}<\/pre>\n<p>Here, when functionA takes a value of one in the input, functionB will run, and when functionB completes, function will run in FunctionA. This is called cooperation. Using Coroutines, you can easily work without using case you can implement.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-65045\" src=\"https:\/\/programchi.ir\/wp-content\/uploads\/2021\/04\/cooperation-coroutines.png\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" srcset=\"https:\/\/programchi.ir\/wp-content\/uploads\/2021\/04\/cooperation-coroutines.png 600w, https:\/\/programchi.ir\/wp-content\/uploads\/2021\/04\/cooperation-coroutines-300x200.png 300w\" alt=\"\" width=\"600\" height=\"400\" \/><\/figure>\n<\/div>\n<p>There are two types of Coroutines:<\/p>\n<p>Stackless<br \/>\nStackful<\/p>\n<p>kotlin uses stackless coroutines, meaning that coroutines do not have their own stack, so fucntions do not work on native crunches as maps. (Simply put, they work in reserved crisp.)<\/p>\n<p>This is described on the Kathleen Coroutines website.<\/p>\n<p>Coroutine is primarily a light crisp. It works like crisp. Coroutines can run in parallel. They can also wait and work with each other. The biggest difference between coroutine and crisp is that they are free and cheap. You can create thousands of coroutines but you will not have any performance problems. See, but if you do the same crispy, you will definitely face the problem of memory leak.<\/p>\n<p>Coroutines are not a substitute for crisp, but they are more like a framework for crisp management.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">suspend fun fetchAndShowUser() {\r\n     val user = fetchUser() \/\/ fetch on IO thread\r\n     showUser(user) \/\/ back on UI thread\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What are Coroutines? A series of Android programming tutorials to teach Coroutines in Android. Coroutines were added to Android with Kathleen. Join us to learn more about this code. Coroutines = Co + Routines Here co means cooperation and routines means function. This means that the functions work in relation to each other. Well, let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":4503,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-4507","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"_links":{"self":[{"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/posts\/4507","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/comments?post=4507"}],"version-history":[{"count":0,"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/posts\/4507\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/media\/4503"}],"wp:attachment":[{"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/media?parent=4507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/categories?post=4507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/milmit.net\/en\/wp-json\/wp\/v2\/tags?post=4507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}