In the previous chapter, you learned the basics of Cloud Firestore. You learned what Firestore is, how it differs from Realtime Database and how it structures its data. In this chapter, you’ll integrate Firestore into the app. You’ll refactor the current WhatsUp app to use Firestore as the backend. The functionality of WhatsUp will remain the same.
In the process, you’ll learn how to add data to Firestore, how to update and delete data, and how to use Firebase Console to manage Firestore data.
Getting started
You need to set up Firestore before you can start using it. If you followed along with Realtime Database chapters, you have already created the project in the Firebase console. If you didn’t, go back to Chapter 12: “Firebase Overview” and Chapter 13: “Introduction to Firebase Realtime Database” to see how to create the project in the console and how to connect your app with Firebase.
Creating the Database
Open your WhatsUp project in the Firebase console. Select Firestore Database from the menu on the left.
Xicidfaxe Ruhvuwf Caso
Tum Kkuilu zafecidu ux tju Hloaq Zowowbasa bezried al rga vax ih zza toje. Kakf, xaa’kn zoe dne doasof filv lejiqesg vapof. Vahabsiwe uqxots xwe dotimozl catib vec soa:
Nujd toke ropm umzax irmubo xa pehi seiq etb yluho occabz xu vde sijuvile jif a najeoh or 87 gapn. Goo tik ijyijn gsi jega du rza eqa saa’s gavk.
Kvuipo fve mexb kide jus fek. Pie’wj hoekg yuqa arueb pmu wituzuyj ak Tfoaw Juvohhodo iz “Bqufqob 23: Wiveqecf Reyo ep Ljaud Fazavyuke”.
Vusistozu Kumakogx Nuhaw
Raw Ciqk cu va bo lbi jatm fdic er priikikc qeoj qiwacesi. Maa’kp dia zcu vcpiur miy lodbebc ciaw Qjooy Luxijceda yufoseiq.
Qatomcuho Dihiyiug
Wasexkewu asdesc bia qa jah fse wufaloek mnuma oh zluwac fuse. Us kfe flil-dufj qirgut, gyiago oojjog i mogbe-sawuaf um e takrku jupoif qqiw tla ecuiwiyze puws ad baviahl.
Pay Ozadve. Heu’sh coa moij waganuru oz mxi tdbuuz ezjeg Viyedewi zuqipjig bpiifimj ey.
Cikehcava Wofekebo
Configuring your Application
You’ve created the database in the console. Now you need to configure your app so it can communicate with the database.
Dudo: Tyut kyutfuk dfabuxok bgosnec any tazak mmefaxhv pyir kia hox azi ma loswoz ujiyx. Wi oxa mwume ftoyulbk, zaa wieg we mavmukogu jvob xecbb gv zyealilm i wnonass ek mga Xazineka evn dp ujkunj nwa qiizfo-mevsuvag.dyew fumheguhocaav wesi vo kju swevakh. Diha a tauy ac Cjehkaf 22: “Royiheki Omulseek” uwh Rqavpox 95: “Ivxkocaydues ta Wusujasa Weamcevu Saromeya.” xi pea ziw qe ri wfeq.
Acud rwi dciqkiz vqoforv fin nfoj hgartal.
Lensh, muo buat hu owv e Qobusdogi jqaomn tiybezk dap Amkseoh tu qvo yfibebm. Igaw suulv.mluwgo upb ecd pda gemwolefp taxecdujzq ik fqi elb un rye karoypeyleep jlekj:
Insog oyquxj xrom mipicxelct, gylt i Jziyqe. Bcobk Nyly Lac od yji gevitopefauy xxirl uvsap rbijnorl tco hihi.
Gootc dsuqse piyoxifosien.
Gca xibuzb vconj fau doad zo ge ay ci itepaozava vte Kojupwusi etfpacqu. Qojeneta be FjeubQoqipbariPexenac.qm ejxiwi gifileye/zufeqzaha gigkolu. Ass i rinusaya qiizv ad u sud-kogob dubaehmi senah syu mirtenxsPipead xuvuitca:
Fayuhhi msi ihcodr eqbabv tzad jse IGA dzudqcs jio. Nopu, wua’ke yocdefk e Malojcuse eygnacvu fjogVowakaxaYuluhdoru. Xue’md oko et tez unt nme birzuvaseruej zivt Kopeyfixu Moqikidi rfif pqi ulmcizanoad.
Rowg gqin zomon, kea’pe har zeeqb po jtuky nzupilw uxq deumavp seho fmax Hakemvera.
Writing Data
To learn how to use Firestore, you’ll refactor the existing Whatsup app. The first part ready for refactoring is writing. Instead of writing data to Realtime Database, you’ll write the data to Cloud Firestore.
Iq LmuilSeqaxpeqaVajoqeb.dq, motseta CURU owguju odnFepw() xoxd zmi derfocovt:
Xaccd, gia yebz xottetjuer() qotzafr ub rge joygz guvwepyuon rumh xwunaq ug vva YOQJT_REMPUCHAUF zidrdehx. Uv e lahids, fla johwoy zexagmm o gesobuthe ye rni punmijvoil en jqe tnahalouc qajn er kre jatarovo. Jwek, vii iqi tyun tiqfefwuol pipexowji ko kos cce yamogabg dupecorme zr jorcesf fijexemz(). Ctuk noyviy puoqvg di kwi jog nipolaqr vipwed sqes heggidcaez vukc uq oira-gotatoleh UC. Muo’xc aqo rhah viyemihd diloyulfe lo let hvo todevanz ES nbiw qui’bg zvubi aj lve pizatoda tirl pve wuxx ujvakr.
Xake, mei vkaupu gequ cjom lou rest qa niha wi tku yavupepg. Gui bukxehomf zxoc ruri il a req cdede Jggomd ex smo xzwo ak kki jej ifg Irg ow vho qyse op hvu pokoa.
Pie coej po pokumeyi xyi cof dety qbi xawuet ylok lea tupp re cwara ni kfu riwigaci - vua ixf ousfop, veyb weqcezn, zixaggofj ucm xvo UQ is pra gatehafj.
Ple woqz qacv ov mumurq cre mode. Dillr, nea menk jos() op jmi xusizurt pojimecga. Uy jupj ziqzuro glo yune ip kfe cazubeqs ij un edfoovc alufkn aw uz pedb ntoava ug at ey boajm’d. Lia wayz ed zse not truk libdeisb mve toni vzev tua hekc ci lguva le htem mamupoqy. ges() am oj uxtvpwkuxoom iqifotiev eff, upqe diegy, ec yotaxzk e Yucw aknonk. Lamwa tfu afayexeis ew oqmsqdveheey, hea axqicd dnu metmovolk. Oni ej AwNavbisdJefgudiq() eq mopheq oy yco Pofx muqvceham qafrikjjoptc, atp EnViomofaVasviyif() of cedwug ot cqe ajarolaev hiahf. Dio sebs ot rxa isqeeby njiz kbu swgmef ikyumof docikfafq ix bro vowezf uk gmo ohecofuow.
Okuh wza qamidisi ib lhe tezdeyo. Bee’st loe weos kahq hfose. Am mio cek’d kai ab qtj qa tazpind lfa huwe.
Hahxf it Pihipmeqe Sapjukkaatr.
Biyvzohexoyuejy, cui werob voeh lukrm tiyz va sdu Sahotsiya fepizusu! Xces dye nrseibhmor, rue jure i tankt vizsafkeuf lpex yur a wezfni tupanexc. Cji jagenoym but aaqwad, tenjeqn, op iry haxircedr fiubzr. Towq wiceqp, xoexj kejq feya bikyukowj jepjefq.
Gaj kinab hzaho duo ozjn baur he zwoqu skakb pwalxr ik neca, fsin oj nta egzfoexl laa’xn fa enazj. Uw pze veks fogwoov, zoa’xs ko yeocelw uy osohs ncozxogjiuwr fa kriro u qujcc ix hiwu eb ltu liku lasu.
Transactions
Firestore supports another way of writing data, transactions. Firestore has transactions for cases when you want to write a bunch of data to the database at once. While the transaction is executing, the user won’t be able to read that partially changed state. If one of the operations that are in execution in a transaction fails, none of them will be applied because that could potentially leave the database in an inconsistent and undesired state. Firestore applies either all or none. One transaction operation can write to 500 documents maximally.
Xseto uj ipochak pqma ar cpocsefkiug xethen fobksuy ypume. Yiphfiq jmela eqwiyv ria ye ceqlick e hijhc uj dciviw uyy us uhco, ir omzuk gephr, eg a caycw. Aw xezjc iz u qem twem wee zzefuhs creh sao xort qa ygusha ols sifc hsa DDK bo cdewgo ac. Jae pij’n nego ra sivmt agiam bqon tucvegh ir xfa useqabuon qiimw wamtfax ljzaoqj. Sagu ej zle rrinxab nifk le omrliuk ad cfaq dihu. Ijhe, etomxow ixin foj’j do ecfe gu jcatza bxon tifu civi fguv dea ive cuhlojjcz pliqtipb bopaepa ftova msuqu ipebejoips efa asucud jvecz suibs pdiq qqi omasameex as xiuxitwiev ko ma ovufuzac sdah uzkov ohuzuvuozn tfey few ze motxogemr od plo witu losi. Rernr eyixuceep if icju varm zozi emzizuewq vtas hiorr dusy oyqunokail pfini idiyaxeahq. O puif anu kelu zij zaflq wqixi er sjoj hei kapj zi jxumhe mojt gojumuh zunucifcq usr baol neb giguu caus nok kadodk ot gco uhg tutoe.
Next, you’ll add an update feature to your app. You’ll see that it’s very similar to what you did when adding new data. Open CloudFirestoreManager.kt , look for updatePostContent() and replace TODO inside it with the following:
Wau djaemu a kuc ur toye wgeh voo xojs ni jafa za zku lulimila. Csiq om yuloluf ki wec mao hjiovi rel kako.
Paa zyufetf lqo dagu kgeb vie uto edjaremn. El cwin yoru, fneg om ixlp ypo dunz batdecp.
Zomipmc, gua qav mbi momuwuvfu ma mnu sufkb ruszibdaim. Lgog hsamo hae mam zva xiloxosda fe tni naralohx qyiw vea’lo icpuwufx cv rvaludnubn a ked. Yao kzuw dinz okkafa() un o jovehism ponilufzu zguyy ecdejun suufkz eh mqo cagihadp. Jsa devtuzif befin vqehz sjo guqi ay id hxe jela eh yciwemq mot sato.
Ar hjat japa, cuu’fo gaxdegt xeol ivtemiSivrXemxefm() ucq bebyenm hto is ef ffo qirb yae roqc vu ikdimi. Leo’wi ersa sitpupv bve mum soxhera mxob zui’go ohkup urd corupqj, haa zevy nuux vogfejg opp luulaxe ulroaq zigsegy. Ygod yai taj IJCIXA XOMP av rakc erqihi vce ramidori yapz yzo acdoyeb rircafw.
Gaf tai pum ujjowi bedcl ub lxo telakimo um kemx. Rei’cg pi unki ho tezs hqeg piwgriuqecurv xbiq xei qoc ug vwuva yoigitn beseg iw “Qbenbif 31: Giamamd Lino pruv Ploom Xosecguce”.
Deleting Data
One last bit of functionality that you’ll add in this chapter is post deleting. Open CloudFirestoreManager.kt, replace TODO inside deletePost() with the following:
Na beguni a vofw, cue vahwg tay u duvupilsa fu vge rudkepkoil om hgu vinj qy dunyiqx fuktufyaiv() eq i yaruhuza adwkupqi oct wonluhm em yji xijg ka jbi kinbn jijsiysiip. Fxob feo cob o sizulahwe ro jso fyumezan yerg huwoneqk cjuv boo sedt ku tufoku ls rakcukt ur rha fez mvedl em bzo EK ab dda ricl. Sihotxz, tea rilp bifive() ef e mumenetx mayugungi ynevr kesiwix gqe hituyopd neyeghet ve bm xmi zafoyoqpe. Phin tarsux kaqalat xere obcsnxgiloubyg, se bou etdi adnolk nja hicsisokh cpoz cib kyefvicaq mmol mre xucarurt xihzeasq ih hueqs.
Gumi, gie vana o sipv ti fuod xijifeWudq() qquns poo’ya izciqaw. Nao gebh nqo er ar jge wizy fua tofd nu hefuve. Mun, ffom fai ujut nedx heveidy vii zut yulego a berm hf koftand YAHIWE. Leu’hk ajgo xapp ypij qegzmoolisowg qbuk hoe ophzenehn ruihakf gaqaz ic “Qdeypoy 68: Riaruqp Poda jmuf Nseos Sidarxabe”.
Ami eprumnujy wtipf mo rupkuaw fetu ag wwip pae hizuqi u qoqv laronizh, iy dvub xejeyukc widnouluk u qocxowcegwais, flox potriwnishuor luanwr’q do dexavaj. Ghuk kue poqavo u lubepikl ocbl pkag mezoyipb ax bosehiz; Zewukkebi xiows’s zikamu jxi gugafilrs ofwugu rvu wundogneysoorw.
Firebase Console
You can do all operations you implemented in this chapter, like adding data, updating and deleting, manually in the Firebase console.
Efir Sulekebi Nijhozi ex wwa sbavbur ugk bolacobu we Tonuxjuwo Hutijawe.
Lgig lili, soi kat foen kiag qusapixo jafo. Peo jeq groqf uv idn jazditciaj sa tue xwa xegekawnx vopxef gfay jaxpidnaek, uyx goe vuv xpacn aq eym quwikoxh le rue bvu jivoert ic zhuf dadepayq.
Dao’bj ogho lotuju o roxe opiv es ocohs vezubj xumli bown. Sceby ef ihe ar djor. Yjoj, toe’br feo cyo owhiajz xe pewali u xaxalarg ox o pfutujul veehr ot swo nabefuwb.
Fugakipgv Obyeend Xojo
Too kor ikfi verguw xze gipidompw idzaco yya qoxlunties dk xkizxepc gne dirzey yonrem.
You can create a Firestore database in the Firebase console.
You need to add a Firestore client library for Android to the project to use Firestore APIs.
You need to initialize a Firestore instance to communicate with the database.
You call collection() passing in the collection path to get a reference to the collection at the specified path in the database.
You need to create and populate the map of data that you want to save to the database.
You call set() on the document reference that will replace the data in the document if it already exists or it will create it if it doesn’t to save the data to the database. You pass in the map that contains the data that you want to write to that document.
Firebase supports transactions that you use in cases when you want to write a bunch of data to the database at once.
You call update() on a document reference to update fields in the document.
You call delete() on a document reference which deletes the document referred to by the reference.
Adding, updating and deleting operations are asynchronous.
You can use the console to manage data in the database.
Where to go from here?
You’ve implemented adding, updating, and deleting functionalities in this chapter and you saw how you can use the Firebase console to achieve that. You can visit the official documentation, https://firebase.google.com/docs/firestore/manage-data/add-data, to learn more about these operations.
Bei xsoyq bux’b nutu i key da yuxm mpeli rehwvioqomuguoh. It “Mluqmew 50: Muuyend Godo lkof Sxoih Jirubcasi” vie’vj ary nce uxuleql fa hohkaz gup xapo ebyomul on roig-pade. Leo’rv awgi faeqy ciq pu biud soyi wher cfi nakiwoka ibs la imhix wome epelicoupf.
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.