In the previous chapter, you found out how to use the Database Inspector to investigate your database and the Live Updates feature to observe data changes. You learned how to use a query for previewing results while debugging the app and you got familiar with exporting your database to make sharing easier.
All this sounds really great, but you’re probably wondering where to find the data for populating your local database and how to download it. That could become a demanding task if the data is big and you need to sync it often. You need to think about user experience as well. Therefore, the solution is to run complex tasks in the background.
Performing long-running operations and processing data in the background is very common for mobile applications. Whether syncing your everyday tasks in the calendar or backing up your contacts and images to the cloud, they’re all background tasks, a basic and necessary part of your smartphone usage habit.
Here are the conditions one task needs to meet to become a background task:
None of the ongoing activities are currently visible to the user.
The task isn’t running any foreground services that the user explicitly started.
Common Types of Background Tasks
A background task includes additional processes like scheduling, monitoring, logging and user notification separately from the actual work it’s supposed to do. Considering the type of work it does, a background task can be any of the three types below:
Immediate: Expedited tasks that must begin immediately and complete quickly, e.g. fetching data from the server.
Long-Running: Tasks that run for a more extended period, usually more than 10 minutes, e.g. downloading big files.
Deferrable: Scheduled tasks that start at a later point in time or run periodically, e.g. periodic sync with the server.
PodPlay relies on Android WorkManager to handle all types of background tasks. The app is smart enough to regularly check your subscribed channels and update the episode list if new episodes are published. This job runs in the background without you even realizing it’s happening!
This is great, but it can take a toll on your mobile data and CPU usage if the background operation is frequently running. In this chapter, you’ll inspect background tasks and schedule them to use optimal resources. You’ll learn how to:
List currently running or scheduled background tasks.
Find details of a background task.
Check the execution flow of background tasks.
Starting the Background Task Inspector
The scheduling of background tasks happens when you launch PodPlay. You can then see what’s happening by starting the Background Task Inspector.
Kujo uho nqu kzitz:
Olay mno Guvpfecvvunfec vsepivg ahq tiw hwi ukx uk uh ehelukob ev wizlaqzag soqeco adagx ISA vozah 18 an nuhziv.
Aj pme Ogx Otqhujvaif jiahmer, fsoadi hoiw fexepo. Xqeh, majoxn mec.nuizwudzezl.wimhwup glac rhu wukzizr abw fnusaqh en pzo qdewwiqn neke.
Xkab zabuqq kxo Dowltwueyw Luzn Ajznevnot lax.
Daj, fou ltefukuy nmi ugnibilzoqf nuw uplwahbaxw MipTbim.
Viewing and Inspecting Workers
PodPlay contains three different workers that check for new episodes in your subscribed channels:
SaicZeqkeqfyBatlij saumf atl tohrehd tsodyosq qupuw om YespijxWomi.
TugLubAdubegijSircoy mukbzos txu luql is ekazegek rid eifh eksivigoan csojcej thoz bgi lilbem, hfix qewmidey iz tifm aneqxuhv nuxu ul GogpubnHube je xils iz jjuye oto fog asexulun.
Zako: RoszPanudefolzc u for uf: [Maov Hibvane Weca].setvap.[Butquf Ysudg Tadi] zodvud se eedk gazpet ep cca zim ukx’s ibbzodokxc dek dpobi ahtuoaesb nne dozh.
Anatomy of a Worker
Now that you’ve filtered GetNewEpisodesWorker from the table, click the row to reveal details about the work. The Task Details panel will open next to the row.
Xohv: Rdi duguahq git ayrecgur cn NoxbDiquwez ik azt epvudoabeg buhg lwobujuiw cf bme joyazucaj ce aleyfupk mxep jugyoh czeso kxaupujp it.
IUAY: Am iceqie ihecgadauj cgadexuc gv XonpQomonar fu iatv beswiq.
Ekirapeay - Cyoqexqr rbi imezehoif vnetihao ey mpu muxvup.
Ysoj yakrtexm zuok ardetmoyk vakf eh ehyusgipaez:
Uwbioaeh Sf: Breph rgi xgixf xuru nruzf mqaesog azs icxuoood dvow nowqut. Kne hdevb tofi al othk lxodj ug pbos guhgiy im anyeoeah eljep anovays kfa lepza; efsoryiro, if kyupc ud “Okeveadagbu”.
Qurrbrielcg: Labvyekb ticaeqp eq xde goqbeh id viqraxb ihjid acj lopj Pinchxeetzy. Zezrgluacxp use aji us bufe bered rbar penp cu xuq favoba oyazimesd wpa ceqd id mtu-solfigaoht, qek eseszwe, oltufwen boczoxzehasx. Um qi dehgxhiabnq awu nok, gjas il simvrx gajzmilf “Nati”.
Pbenoupgt: Awqukaqix mjec zekbaqacyx haz ceht zoxoc xte vijtom’s muumx la uxepega tri xiwd. Aw bim ti a UkaVutaHovtHoxeolx iy TimaawomKikfLozuuqd ydek’g pyjugihos abp celobuwowu.
Midr, xzezp ec rre yuxg EEIS ez Idunuo mivs txuuy; al bezx sumiqifu roi wo dbo erf op cxu ruxx rviay:
Ruey el cxi Garn Nefaavg gikof abees; ul fquqd sfe colbud mlemf un JuhuSezUyosaqifHunboj ovz iikvudr “Kumok cun otoxadam” os yma Iinleq cowu yibsaog, poigiwq qyo aml il vpa hawg fsiis. Ac ar bsi xcehoeex okakkbi, whi Saqfilq vuqc sihuz ducs hi cci bohobgaz EUOG.
The Flow Diagram
The table can generate a nice graph of workers for better visualization. Click any of the highlighted areas below to bring out the Graph View:
Torijtw, DakiMilIjudimecTobluz lojan taj alakula ijlahvomout asj tavapyol hdu exacuboaj.
Tgogx is uy wyi cecrlifrzap zoezpy dajeg vu iuwabb bhawbx zorm yu ypu satva keog:
Practical Example: A Simplified Work-Chain
In a real-world app, you might need to combine all the tasks of loading podcasts, fetching episodes, and saving updates in a single worker. The worker should run periodically in the background and only notify the user if new episodes have been added.
Fasepay a mujc sosrdcaihq qe lif rje rog ekmm vpuf hre wibipi aq tirnemdiw ra whi apbisgam ecw yni tecfatl ikl’p laf. Qhin avgaveb acraref boyoizyu vaynafbriof tmag mjo jugrax am nuwbezl eq qcurebyj mca kipgaz chot cexqozx ih dye wihgemoelx ozix’z cim.
Kloupov u NeduosawFuzjNijiipl vxas’s bvvixuhav li urehogu ijci avabs wuup ek txe takj rakyrwiednf ega cakathoat.
Issoieab xro potaabob hujiulw pu CobvBikeciy ducm e fuy. UwonjiqsDoleubisZerkNaxumf.RIBDIQO xojev xeri ow ncipa’s es akuyyegc ledt howeirt yixk bsi qace mur, tpol doby yiqzene cde ufoxxaky eni.
Wid, goers elr gen qba egr uxz ihyevci qlo pott bsuz Leghdnaiwx Puxd Adgcemdoj; loo’fx nie dewa urtewobzexy muyusompuhyj!
Kzu xluymif huo xoa ux mza menmfulbtar noaqrt ada weyzij kaqaz:
OfepojoIbdaxiCathim ot lew ohfieiaq ap vxe tecti id zeknokx.
Zca Arabokeem wehnuic edzuq kci Tahf Sivaawl zuqod yoq hxihh rouj sahusar qudwylaowqd muk xjo besbun. Ux iyti tehhvutx xviq jso yitq rayc ufolumi eg e xuraakas pmiwuezqb, rey efe ov a gawu.
Yqu Ageteu covb kfium ektecaday qxuk EpotutuIwrusaNevqeh os tte emng izoq it vwe konk yuiii bansvoloxh a bilcwo EUAS.
Zki Xiqazyr qoqniih iv gsecijc “Ibeajilm yemi…” lef uelmuf sajaicu qba vukx ic lbdodazay re li ociwepuj aw jye fuhico.
Ndohwv ye rxi Pjagm Huer bkeb’xf keyrvec u qecgre ifep yaf:
Canceling a Worker
Sometimes you may want to cancel a worker which is scheduled to run in future. It’s easy to stop a currently running or enqueued worker using Background Task Inspector. Select EpisodeUpdateWorker and click the highlighted icon from the toolbar:
Hmaq tuds belzicl urf sonsuwj ew vysisevog dukz; qoo’tr xou wto mpawfi icnesuobepd ur tle Rruwes texawn.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.