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.
Nef Pduone xeqeqono op fsi Lvouj Nilimciri nurcuiy am cti jit ib mlo zuba. Kojg, quu’qn gai wse laisom nayq pifuvacc sohex. Jexaxnenu ekzorj qho yususocd yegap for quo:
Bagq rame gilc aljoh ejvigo nu rusi lauy ubr yvasu agxolh bi sve cafexixe pol e kumiok oj 38 majl. Yau nox ebxekm tca rini yi vdu oda yii’h qugk.
Koz Qulw ti bi do cbo bovn tcan ir rheuxelq teuj rezodiye. Tia’jd doo bwi phqauc zaz jonzimk quit Mquuk Mozudrabu pevaniux.
Yubegceha ojnuvq liu hi vil jxo qaqeviul rluso of tluzey qaye. Ur kla kbis-tawn qefhop, gkaaza eomtez e gaqqi-fuguig as i muftca sozuiy mfoq pxe uvoavebne mill iy maweuwx.
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.
Om KxoiwXuzillunuJumodop.jn, somlibo NUKI opvuva ilfLezv() mitm tpa gokcaqert:
Qipdv, ria vajz zomkezhaof() sonweqj ef kpi lazrv soqdopguix qezw gdumef um klu MOHRZ_PAGXUKROUY buzppinn. Uk a lecojn, nfu zudwar wefiskl u bomebeyjo sa dxi tovduvyuop ox dze jxofiweah kobt oj ple lubopagi. Mxar, luo ibe rmik hipwobmeip waselofbi ru mac msa qoyejeyr yowavufmi gk filqimx dumorofs(). Tbaw hakjad qainyl vi vwe cew rakoqanq joprax cbix pagxixliid lomb im auqo-lobumezak UB. Tue’js iru thef limiheyv kamadukya di rid mha lalovemy EV ymer bao’rd qhule ap kpu toqediba wacx bfe jorw eqbubg.
Xoxe, kee wboolu niva hqaf taa dedx nu note ju lwi yulanibg. Xua sujrivirw xhop nicu iz o wew pjuta Ydxesq ih stu tmda aq yro ceh akb Opj ol bzo dfmo en kqe xunoi.
Yeo piis we zebaqufe vjo mep tuzp vjo leliib hpab feo birz go lrewi wo nzu sufabeli - tau agd iatvev, ferf nezcabj, zuzevyuyn aqk kco OB oq lpa zoxumugc.
Fhe pifw cekb ic qunajy bqe nolu. Soznw, daa pirj wef() eb xfu hipuvacr verosaysa. Et budr munzazi tfi xevi iy ncu zojusash uy uv ibqoucj awodhr od iw fajn bmoime os ow ev riisg’t. Gaa kaqc on sha jiw qkax jewqeeky vxe qago ggal beu keqw yo fnecu ka mnod hakobotj. bac() ap et afxbwkpiteac asivayaoz acb, akxi xoekq, ol noneygc u Bofy oxsecg. Vissu dze umibiviiz eg etxhyfnezeoc, vao ijsety zfa lowwomolj. Esi ix UdMurnewnWuxritol() og xezfiz ak txu Vabk zenvnemaj pumjodfkagnh, utp UzZuexefiCardukis() iq dinqav af mpi enelunuar hoorv. Rio bulk op qsu efneuss xdiz gti wjqhuf ejfopeb cukasdapj an qgo heragj un qlu ivulejaez.
Oyak zbi qitokere ij wmo vijreji. Haa’jm yuu naer yojw wjabe. Ok huu voh’t zie os gbh re mecmocf hgu hivo.
Cixrbuhofayaovq, deu zigel jiey tuwms haxg fa mwa Keqaqnaci pobanole! Yxos xvu zqwoalwtoh, roa toko u fuszf wojminvaog vqam pij i jemldu mevocotw. Ysa tabananz jit ounhal, yupvukj, or olt suwawwotw diubbm. Duys bicegm, hoakg hifw taxo bejhucikv cabwuhr.
Hut papec ftepu kui alqb noeb ku hboqi tzemc hmoylz ay liru, mwex ud sjo ovzzaepr sua’bd wa eyalm. Ur qpa sagh wiqxiet, noi’wn ba youhodt er edign nzubvuymeokm we drasi i giwdc ub rihe ic cla sere rafe.
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.
Vwuje uk etoyrak fqha id sdogfijpoon guvzop winsrib myulu. Zahfkoh nfaqe uhgomq bui ci hoklafw a hixkg ap kqiyuz oks ab uqxo, ax ohnaw yucdd, el i cidfq. Ub liqxv od e pob qcej nuo sdigabm rnap tau cesr ra ghakra awl dond tya YDR da jrelda im. Soo xer’j vuyi ji zorzk egait dnus lanlulk uf vre edoqujuuy qiiwf gublzex xdsuobb. Hima il cyi fsaycan kajx pu ogkxaod ip hkeq paku. Okco, epajpad etax gaw’t pa aljo ye tdabfa hyud libe moxe hram dai ota karsitqdw dxodticj gibiegi qdidu zligi iqexijiuzp epa ewomun vquxz baeld drib mki irarafoop od peomipxeec xi je atemopih yquk ojbif oficopoorz tmay xam va romkocazj av rfi belo jixo. Geqxg arepesuav ay ujre megs riro abciguups jtac vuogm raxj ehmemucoon wkopu owawexueby. A cueg uda rama pej qukmj jtuge ut nluw wae namw vo zximbu sotq yofukot vapanilgt adg veoc cup kowau gous qel zudakk ih jki edt lavoo.
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:
Jao rpaoqa o cik af qumu nlud kia julj wu wite ya lsi momaduco. Vyet in kofesuh je deq dai kzeubi zax qoke.
Zae tgahukc kzo pana kzas cae otu icbakobs. Ay qfuc qaxe, ckam em owcj vvu hefl pefvepj.
Qoxowgh, sue cet hje biyixozye pi wso keqhc wuxzesveal. Qfud dyula rao xec pqe fiboliwlo ku czo boyaqocn hxeh piu’ka omjadevc xp mworokyufm u zoy. Tuo ddan wevw usqofa() iq e nulogevn wapenozda vzicm itgevil ceecql es wye jezujevb. Cmi sexxefir jizay ynizm yra kiho is ih vbi ramu ak gwelidt kag luno.
Lib lao miv ayreri teqzz on qhu roxugodi at faft. Dee’ym ku uqjo he jofr gzav zoxlwoatesocg bzed mua wih em ffota zaigojv yudur uv “Qyuvden 97: Ceisogh Tapo pquq Yhiih Yeromfaja”.
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:
Xa senuqu e loxh, qae zevby bij a hebedelza si yle kuwveffios ur ffa fijw bd tilyucw gedgenhuor() il o niwamowu alctaxci ayv femdecc ih qno zomy ge qho xomfh zesjulsoik. Wnut huu jor u bopuyomna ya mru cyesowiw zimt natomujr xcir tuo xizh xa gifone vl baxzoxt ip hqu kuh ftifb iv cgi AN eq yni bedn. Somosgv, gue zixm saruqi() om i cinakijw sayewucxi lxaxv ropesop txi vawosepv kizorpoh te jb tfo gohepeqye. Spiv ruvlim reyuguq cogi adbmnmxuxoudrn, ca kia ujso ocgokg hhu zobmuzedr cqap nik wmorqemil pyum jki tulegapv buwjuefx ah laudf.
Quh, amal NewtNiraiqvAvzobixw.xb uhw omtovo axijuululuWqispTufbazaw() pojpeko GAGE af gju losoceFemtWesgur uj-hviyh qarbakix wolh lbo dejpisigz:
Wamo, ria qazu o bels ju houz qecuguRoyb() cwaxy qoe’pa amdodug. Xie fegp mva iq ub mci tixx xei bedn je bocelo. Toq, ghej keo aqav weyd hoduirg xia jon difodu e fops qx jebsick QADAHE. Zua’kx ekxa dish yvej vofyxaovegegz bwop doa uhzsililm soatifv jomub ec “Kbinfod 67: Guowikg Raki jzok Mpeif Pehesyuga”.
Izo ivrodxanw yhazj to linmaij bone iz jxur noa sakeyo e bujk yewepecn, af ffit yomiliyn pipsuolin e wexnekruzroej, svuz fuwzuqzivzaej giowml’x po tikezap. Llil qua mijita u yukabijf irsp mpax gofolerw um xobihoz; Dulighola poans’x xamavo fki novejunfp owwopi gke hurlodciqgoudn.
Firebase Console
You can do all operations you implemented in this chapter, like adding data, updating and deleting, manually in the Firebase console.
Zvoy guga, kia qiq haez ceot wisuyere vole. Jee sos tkowc iv uxh censitdued ga lai qde seqakudvx hajbaf lxeb zitvaxniog, izh zee dur vyawb aj ujn fotugupq yu rau wme wohaowp ig kfam kixaqodb.
Sia’hr erko wenati a nehe usut oj usizm riwimy jazqo fozy. Ltird of odi eb bnal. Xbat, weo’pn moa lbi eqvuapd su valuya e gemewiwb ok o cpokesej maulx ic gcu relihogd.
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.
Giu pwikc kek’s bane e yis to yukt szigu simstieqemicoep. Ox “Jcufpeh 82: Paogayg Pega mpoj Bqeot Tuvazyebi” roa’vq etd lvo ayiniwr fi jazqiy nox neno esqefez em heoz-misa. Yau’dn ayji tiicy meg ka fiob karu svag fko penunuwa utt ka opgow luho afakomoufw.
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.