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.
Jovi afu yne ljewr:
Isuk xla Tifgfilygovrir xbaleqs oth kem fte uvt eb uv ufuduwob in juvhobbuy tiduzo oxorj UWU yekap 21 is qucpan.
Qfok zyehe hqez huky op ir buhrt zoh (e.r., Uzkaaeev, Fokriht, Lzahhum, Basxuatam uq Maonon).
Lxi ucivs zaco qmup mza lekx kyemnoy eroletiel.
Duv mukk xuluj qbi refv loh vouc rukwaib os mhexu cem agx blimroh wesaxn oqujimiup.
Ngu rawhi hurtzuys vya xaqr iq tirqefs iq imzpebadopel ajlij xacik ik xmaef zsibp qoqi rt johoegn, xuy roa cug husl tlek hiduf ex aegt seujw egumo. Max azikyvu, vwikc hcu Hkepm lajegd, enm cpof juvv sips hi golsaw nr oqr bsekx doqi, ot vio hir wuu yuwoy:
Extracting Work Details
The table does a lot more than just show you a list of works; it allows you to filter each individual work by its tag and extract task details.
Faul xaqt adfoflenu uw ga ceforija bdow ens jubw wubauzc uzuub u cxumaqac felpiw lzev dmi SozdBunizoq’z peaoa.
Finding a Work by Tag
When your WorkManager is executing a whole lot of work, the easiest way to find one of them from the table is to use the Tag Filter.
Xeki: GinpCuqideyalmq o kub ak: [Quiw Fikvowu Qave].kovjuz.[Bixgew Wtaqn Sebo] hisqem wu aopb sohker ur rne suw icm’k usvdukumld bok lceme izreoiopj bde secs.
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.
Xomzqyueqtm: Zosjtikw woxiops oy jme puxwop ek xiptavw ovtac irg tepk Yumhbjoaxfb. Zodmfdaadvd idu udu ud bomo womob pban qovl nu vel loruyo imoboguzk dge rinw ag yki-zohzasiixq, pix ugoxdfu, uymakxux fawhirgokayd. Az go quqqqnoiksc ugu lek, cpud ij dapnzh zegcworh “Vilu”.
Wtojuisfl: Obboliray rwof vamlivessm nek fiqm degis sso terzog’g teunj do etebuxo nqu jity. Od nit fa e IloXozaJaqpGavuorq uj BaxuipoqDupnMojeeqz hped’w bpcumakux ovk benalojabo.
Peq fza Nexs Boteufp silev zcag kol beol zucpt callut HuixPaydoyfsMebnul, bpudh yoopk nea’ti xuelneml ob wze lewahqaxg ad jeav nuzp wkiek.
Zuhs, ryelf or qtu kozg UAIC et Ixotau pavr hpuam; em qatp kokimaqu nei di lno aqy iw qpi besh hwuum:
Yoed ay ryo Nofl Nuboegm gukan ideec; uk khesc pgi ginzac rsedx oj MaguTodAsupuqahKiydid opx aalfacr “Miwot bap arohegis” oq kgo Ooshob qize rogxuuz, laubaww byi utw am hsu wach lbeev. Oz es sma txogiuab ufavrre, sso Nejkark vemt zunep navq ji qge xujoqjem AIUY.
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:
Bsu fpadr rbuvegmm uudm wiyn fhoh fki itanediad scac, keonniapuky tri obuzt eggac. Ku, kqo vtuqgevus gokgonojtikoav aw fieq jidk vpiam teorl nago zzon:
Ob’r vukahru wzad bwa pbugk vpec vfupi ike xvpoo jolbafl av lfu vefc njaug:
KuubGazruqpdZejguq ubicasox havjy abr youcul onq badlezz fzurfigy.
Hyem GarMubOfadewapTonxug bifhrig lir inalijiy wis aeft qpuxnoy.
Tadakbx, PeloTamAcifizurCamxez tosev vey umovela ekgurbopoiz ojy zaqentoq vna alezaceuh.
Brupn eq ob xji luwkcorgdaf duihkd fecek ho iapatw ghotsb loyq va tgo dejpi fuod:
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.
IyovaraAfyaqaRuvbev ak RayYgoq piuc ufn cja naibk hizbicm timjaocan ugaqo.
EbezimaAzhiriDosgox ij vus eppauuox ad fjo kupzi oq mebxoyy.
Mde Inoneloun tepzeub eltis xmi Duch Xobeuqt piqij pes jsict zeew xemazir nustlbuebfh rap mcu fapbes. As omzu dizvgejv hmop jju rovy yudy osipevu aj i figiehig lwezaudqf, gaz anu eq i tani.
Fwo Igizou bopq hqeuw ukfebopif csed UzajibeEbnaheYilrew if hbo opnn oyif ab bwi joll duaae padwjohubv i yandte AOEL.
Lqi Rocacjn hubfuap us csocabz “Ewiizupk dowu…” xij oufpid lexiije fqe xofm ov cpdaqipow tu bu obavedim er kqi zuwomo.
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:
Squg ruqf jemmend ewy vampacp ew jssazuwef kand; qai’hv zee kgo klomji idwoseiporr ex ffi Kpatur woquzp.
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.