Cov lus teb luv luv rau lo lus nug no yog: Yes. Tsab ntawv xov xwm no nthuav tawm Java ntshiab kev siv ntawm coroutines, muaj raws li Qhib Qhov Chaw ntawm GitHub hauv qab Apache 2.0 daim ntawv tso cai. Nws siv cov yam ntxwv muaj txij li Java 8 los ua cov lus tshaj tawm thiab ua tiav ntawm coroutines yooj yim li sai tau.
Can coroutines siv tau hauv Java?
A coroutine yog tus qauv tsim qauv uas koj tuaj yeem siv ntawm Android kom yooj yim cov cai uas ua tiav asynchronously. Coroutines tau ntxiv rau Kotlin hauv version 1.3 thiab ua raws cov ntsiab lus tsim los ntawm lwm hom lus.
Dab tsi yog qhov txawv ntawm coroutines thiab RxJava?
RxJava siv tau nrog txhua yam lus Java-tshaj, whereas Kotlin coroutines tsuas yog sau rau hauv Kotlin. Qhov no tsis yog ib qho kev txhawj xeeb rau Trello Android, raws li peb muaj tag nrho ntawm Kotlin, tab sis tuaj yeem muaj kev txhawj xeeb rau lwm tus. … Ib lub tsev qiv ntawv tuaj yeem siv cov coroutines sab hauv tab sis nthuav tawm Java API ib txwm rau cov neeg siv khoom.)
Thaum twg kuv yuav tsum siv coroutines?
Siv rooj plaub: coroutines feem ntau siv hauv kev ua si hauv kev ua si rau lub sij hawm-slice xam. Txhawm rau tswj hwm tus nqi sib xws hauv kev ua si, piv txwv li, 60 fps, koj muaj txog 16.6ms los ua cov lej hauv txhua tus ncej. Uas suav nrog physics simulation, input processing, drawing/painting. Cia hais tias koj txoj kev raug ua tiav hauv txhua tus ncej.
Koj siv coroutines li cas hauv kev ua si?
Ib txwm tso coroutines ntawm UI txheej ntawm kojapp (ViewModel, Activity, lossis Fragment) thiab khi lawv rau nws lub neej voj voog los ntawm kev siv qhov tsim nyog CoroutineScope.
✅ Ib qho zoo dua solution
- ViewModel. Thaum tso tawm coroutines los ntawm ViewModel koj tuaj yeem siv viewModelScope viewModelScope.launch { …
- Kev ua. …
- Fragment. …
- App-wide Coroutines.