協(xié)程原理

welcome to the co-routines codelab ,in this codelab you will learn the basics of using kotlin co-routines and android apps to perform things like network request. we put this video together as a quick introduction to co-routines . on kotlin if you already know them from another language feel free to skip it and go straight into the codelab. co-routines are a new feature in kotlin but they're actually a pretty old concept they've been used in popular languages for the last few decades and they turn out to be a great solution to some common problems that we have on android co-routines allow you to replace callbacks you can transform callback style into what's called sequential. programming in this video wo'll explore how they do that. in addition to replace callbacks on android they also give us the ability to make our code main safe and this means you can write co-routine base functions to make a network request for example and just call it from the main thread and everything will be okay. so let's write some code to make imaginary network request to make this interesting let's start it on the main thread if we write it in what's called a blocking style where wu return the result directly from the network request it will block the main thread this is a probleam because a network request may take seconds or even minutes to complete and the entire time that it's running the mian thread won't get updates or respond to touch and the user will see your app as frozen this is a really bad experience for the user and wo don't want to do that on android if instead we write the same function with callbacks now our code won't block our networking library we'll take care of running the network request on another thread and when the data is ready it will pass the data or call back to the main thread the callback is the handle the library uses to pass data from the background thread to the main thread while callbacks are a greate solution to async programming they come with a few issuses they don't handle air as well without doing more work and they become overwhelming when too many callbacks are used in the same function to solve these issues we can use co-routines here's the sacme function written with co-routines . to make it work with co-routines it becomes a suspend function it looks just like it did when it was a blocking function however since it's a suspend function instead of blocking the main thread when network request is called it will suspend the co-routine this frees up the main thread to handle on draw and user touches and when the result is ready it'll get passed back instead of useing a callback the co-routine wi resume. now it's important to note that with co-routines our networking library still uses another thread to run the network request just like it did with callbacks the main difference is the code tends to be simpler than the callback version now you can think about suspend and resume as replacing callbacks so when you call network requests suspend makes a callback from the rest of the function so how does kotion do that. let's take a look at how it executes when kotin executes onDataNeeded it will execute it a bit differently on the call statck than a normal function it keeps track of the place it starts the co-routines so that can implement suspend and resume then it will call it just like a normal function executing each line of code untill it reaches a call to another suspend function before it calls the suspend function it will copy the current state for later and that's how kotlin implements suspend you can think of this state as a callback that knows how to resume the current function then kotlin will run network requests until it suspends itself to make the network call now at this point all of the co-routines on main are suspended so main is free to handle ui events fase forward a bit until the network request comes back and kotion will resume these co-routines first it'l resume network requests as it does this by restoring the state it's saved earlier once request is done processing the request it'll return to on data needed and the result is available immediately if the network request failed it would throw an exception right here instead then the co-routine will continue executing just like a normal function until it returns because co-routines allow us to handle long running tasks without introducing callbacks they tend to create simpler more readable code?.in the rest of this codelab you'll explore how to integrate co-routine with arc components to make a network request we'll also touch upon how to make code main safe and with that let's suspend this video and resume the codelab

suspend fun onDataNeeded(){

????val result = networkRequest()

????show(result)

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末现诀,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蛇尚,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)净当,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蕴潦,“玉大人像啼,你說我怎么就攤上這事√栋” “怎么了忽冻?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長此疹。 經(jīng)常有香客問我僧诚,道長,這世上最難降的妖魔是什么蝗碎? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任湖笨,我火速辦了婚禮,結(jié)果婚禮上蹦骑,老公的妹妹穿的比我還像新娘慈省。我一直安慰自己,他們只是感情好眠菇,可當(dāng)我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布边败。 她就那樣靜靜地躺著,像睡著了一般放闺。 火紅的嫁衣襯著肌膚如雪祟昭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天怖侦,我揣著相機(jī)與錄音篡悟,去河邊找鬼。 笑死匾寝,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的急凰。 我是一名探鬼主播床三,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼杨幼,長吁一口氣:“原來是場噩夢啊……” “哼四瘫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起欲逃,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤找蜜,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后暖夭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锹杈,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年迈着,在試婚紗的時候發(fā)現(xiàn)自己被綠了竭望。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡裕菠,死狀恐怖咬清,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤旧烧,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布影钉,位于F島的核電站,受9級特大地震影響掘剪,放射性物質(zhì)發(fā)生泄漏平委。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一夺谁、第九天 我趴在偏房一處隱蔽的房頂上張望廉赔。 院中可真熱鬧,春花似錦匾鸥、人聲如沸蜡塌。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽馏艾。三九已至,卻和暖如春奴愉,著一層夾襖步出監(jiān)牢的瞬間琅摩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工躁劣, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留迫吐,地道東北人库菲。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓账忘,卻偏偏與公主長得像,于是被迫代替她去往敵國和親熙宇。 傳聞我的和親對象是個殘疾皇子鳖擒,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容