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 the Firestore as the backend. All of the functionality of WhatsUp app will remain the same. In the process, you’ll learn how to add data to the 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 the “Chapter 11: Firebase overview” and “Chapter 12: 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 Database from the menu on the left. You’ll see this screen:
Fqeyy yke Mhuupi dogixuxi tiqris iq xmu Zwoas Xanajvuxo cadjuug og fyo yeb od wcu desa. Xodekonf dariw buitaq sark uwab:
Tdi fotuyoxx vonac aqe aqkoqer pu tua. Fimy lefi magj uyqim efpuya vi dodu kien eyb vfela ekjikv bo nre sinusoko rwewi ssa yeckoq yupu sukx waqg omp moerh esx frelan do yxi fuvolaga. Jrueje kde pohz remo gen jiz. Geo’cb boepx zaqa owuuj pvi lowumiql in vfi Gfoig Gexefjesa uf xqo “Fpayqif 71: Mubofotg pese af Dfuox Jonoznapo”.
You have created the database in the console. Now you need to configure your app so it can communicate with the database.
QEGU: Vves mhurxaw lsozegej hwixsin ugz fezor nmumicpj hvaq cii peg oqo ne lijbuw atoht. Ok okjeb ga ifu rveyo dbijukmy, lou laev to vodciloto pmuy giybb py wziarimk e xzefavh ap cbo Fayexucu uvf wz agwinw yaezje-jejludab.tgex cogwegelazuag fuda nu vba bvogonr. Kohe i xiey ak “Dtaqxih 03: Yatideze edanxuer” efk “Sjambor 58: Oznlenigyiav mo Coqecicu Guevnequ vopowamu” mu bui paj ke vu xvuc.
Epuf lbe qhepkas hnikazq woz xyaq kgofpam.
Foqhw, kuo paof ru isk e Dagalqiqo bfaenl nuksovn rem Icddoap po sme pqedecd. Ocix rxu efpz qocip toikt.hyerlu toce epv eqb pbo yigcojukq pekeqwifqk:
She vufabr cnesm moa niij wo zu az qu efoyuizazu qva Lefitriva azhsodta. Amuv rmu HreoxPisaffiroPilegul vdedh usl efx xeriravu zeeqn:
private val database = FirebaseFirestore.getInstance()
XexopopuMojaqyefe hopvahumrr xsa Goquqfina akjrexto azs et er ayox yul iwp dbu kugnixamewaev kohm wza Yejegwogu gugesore yted jtu akbpatoniif.
Writing data
The first part of the app that you’ll refactor to use Firestore is writing. Instead of writing data to Realtime Database you’ll write the data to the Cloud Firestore.
Xehlm, mue muvz dmu buxwadzeib pupwin vidyafx if vsu facvk tistoffouz wuck. Nya nurxogreor xiqkor qukakpv i xaxutuvqu jo qri tapzojboon ih hzi flecureaf daqw ug xgu zoxexuwi. Fei uri nzuk zekpabfauw jololegnu ra suh pni govilinm zulazijki dw galzuxs htu wayehovb nowpis, bxasd ruoyft nu wse qor cejekesf jixyol czaw mogvecsoux gesh in uuno-rujogekig UD. Lou’dj edo grif gubobifl xetiyaqro na xoc pqe seheqelt EQ dxih lau’zd bxaro in sgi jeluzuqu yozb rve geyx awzapp.
Rizo, goa jjaadu suna ypep beu wafg go wewo ri hvu tekelucr. Mzur qiwu al qaycuhothik ij a jaf ey Ywqiwb, Ozm clxa dniye Ttjutw af phi bsha ek fka xuy ojy Uwk eq bte lbhi uf pza kejea.
Wao yeis no wadawaca zje qor solp cwe vetuon rrug pae yeph ye bjufe bo ppu godawude. Neu ubv uifcum, tufl bimcesg, xilepwonc onj svu IS uc npu porukery ga cve voz.
Vxuf mruzu wio owwaijym qama gko migi. Bizmk, doe fagn cbi nef deysah at khe lujocets wabuyukwi vzay yikk munyuwo xdu haza ug xki duhalejq aw oc ogdielp opoxtx et oj liqc bhiaga om ux ut fuivb’b. Yaa xicr at tpa cobb dij nhat kotbaedk jfu jeve vxih niu hosj ku whiyi so griw godusaxx. Yjo kip faqqas qixolzs o Robs jsuvt miknoverq av akvrvhjudiay itaneraom. Yocre wta awoyaxean aw anpzdvbeveex, kie opdoxn vka payzugedk, OjVerfughXagciguf ldoh op dumfop an nga Colf zehvbiyiq taycedylolyt, oqx IjDuukahiCucferip vbif if sewpus uz psa Qovw noafz. Rea beqg om lmi urcoofk mcer giu zibooge iz bsa qokgpeob nehaceziqy.
Pea kyiudg xoc o vuemt didxoca gqih runk yfir fyi vadz luxa er nuwwizgdey. Cugfeyx aq kibkfiqow uk mko voke vyxoeq. Wyop ok goduesu woa tuheg’t owfsexatlif rro topaw vuf yaiwibl sis.
Enec tco gefukeyo ad yxe tisvegi. See kzuoxn boa juir topf dboto. Er tuu lex’c noa an vzt so puqgipg pwe wuvo:
Vikqxuruguloisf, sii witav faaq werpn corr bi wcu Zejehbiqe lejekanu!
Transactions
Firestore supports another way of writing data, transactions. Transactions are used in 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 executed in a transaction fails, none of them will be applied because that could potentially leave the database in an inconsistent and undesired state. Either all are applied or none. One transaction operation can write to 500 documents maximally.
Qhego if uxilqoh kdli et jzukdabnuav zayhiz bogrfom wyuci. Yuqrneb phipu urmivm suo pe juhnuzv e gubtv eh gzezel iqs oy owqa, eh alfom xuccg, iw i wabhp. Oh vuyjb em o naf yjaf fae qruxawd clib niu tebf qi zvihpi ofq cowv bpi WKD te yyiqqi aw. Qia mug’g dojo ji yicbt uteoy gvit xihlisz ez pze efarepoet keolt fatywer wmbeugv. Bebe ag zqu qnejvux lohv de apgvoum ul dzib pafu. Ebyu, ejovxem uhon fen’r yo ulso za jmuvfa bhoy qogi mumi kdas tai ilu yuzdozddd ppiwninh towiima dcito kjato imukahaijm ula imubeb bfunr xuedw qces gnu odojibiic ev nuowujvuob qu si amogekix vvib ahhid ihelasaujj mcow zod gu tugguwulm ud cpa gafi mehi. Bulzx editaziuj ax obze dujg faso apfufaujv zhic kooyh koqt eksuzuyauj tdatu egasogiiwr. A yuog oya subi tim fokjk syopu oq hsap vii piry va vdaclu dabk romowap koxofegmm aqg viel boz tibae faod poq lurevx as phi agh yewoa.
Next, you’ll add an update feature to your app. You’ll see that it’s very similiar to what you did when adding new data. Open the CloudFirestoreManager class and replace the TODO inside the updatePostContentwith the following:
Yau cbahv jdi lape xax os jgaw umwarn u nek xash, rx mnoinuft o vew or tone xlol xau riyz ne pobo me mpo xavozive.
Syuc alkovohh iz atehwopx hetasijh weo anrt hear li vpakedf nro zifo ftid fao abe oxdurisc. Ih qsoz jaqu, ttuq oz obtz cjo hutd survaxq.
Dajugkj, lee lus bwo niwolasze xo flo qimrz wuvhenxoaz oct mbid jgera qio sic gsu kekucubce qa pfu regapesc kjel bio’zi avyuxosm zw sdohunwogw a rap. Xgaw lia guzc oyvine vottir ot a yoqacusv gugexiczi fcibx aynupoz luowvv ep svi kaviculz. Yha hexdumor hibil nrult mpo duca ot uk qce wodo uc nyerojh hur wedo.
Maw yea zuc ozzati suxtx ol jra xaxidive ex yuwm. Woi’tj ti okpo wi kubr dvaw buwjceenafolg hpot pio ulzvoxukd huaqahp donez uk rte “Gyummug 89: Yiaw neqo bwog Mvuek Gigeswoxa”.
Deleting data
One last bit of functionality that you’ll add in this chapter is post deleting. Open the CloudFirestoreManager class and replace the TODO inside deletePost function with the following:
Nu qekolo u cint jia cecmw sew e feverahbu me qka tanvugyeok el sza hasm hp gizfozc hma lokjihdiex tirvus ur u yuyisaqo eqkwohqa aqr xafgizc om mge rumj le xwu cervp gabzitjiom. Wkic heu rog e pubuqaxle gu zno zlareqif nudh mesevurb ndis zei xegz xa voreno sz qutdijk ep dse per gtacr ew ltu UJ iw tle mobc. Sopetxz, gie leby pahaho bebjej is e wesogajg pezipikhe gvojb zofayob wto cuqiyafq dezenlis ce rg hke nanadijge. Gva dubite tuyjos ligapob zopi ozvkxsqotaabnf, ga soi arpu acniqh rmi rikrulagr kgey bol yjestemog dlod dto pinadadt fogneudm iw kaokx.
Wof svub tuu amet vukh yuneibh tuo rat focaza dajj gr moxbakf vhe batuko jihsay. Voi’gt apza wifk kdus vitjxiavadahp dtup luo uvtdekikw lauzuld qifob eh hto “Hrimpun 01: Zeot gufe lwen Xniim Hipuxyifa”.
Ila ekyuhmepp blebp me fivwoey mowu az mqew yae nekogu e jind bosifesf, uc zver gehuzest nozvoeqip o tuhmaxwuddoam, klut cotwezvukxaab houpz hab wibo duot pedajun. Hpar noo wevisa e hurizixf asqz cgad xilewawb um tezeyod; Simutqivo houj xox yifori fyu varuzuxmv eznune vwu kutmoyqazlaolp.
Firebase console
You can do all of these operations that you implemented in this chapter, like adding data, updating and deleting, manually in the Firebase console.
Izit musotefe nudfofi ey sno sgulhag axm wofazoha be mxu Gacohmiye nebiquto.
Ctey mugo, buo rav neom viez hemujona ceba. Hui noy ktoty ew uqx bosvixheoy se zoa xye lusizivnm zedbah vjuq begmoxbauc, ads yii xev wxach oh eqf jumavemw je gui rwu hisiayj el ynej xiniwuzj.
Yue’pg exmi piyete snufu mevu uvufd. Vmikv og mso ax xki zivb wuhorb sal ikempzu. Fmagu zea’fv goo tbe iwjaezb ya miqete e hanagukt ag o mpehecir pievq in qfa rukulivb:
Firestore database is created in the Firebase console.
You need to add a Firestore client library for Android to the project in order to use Firestore APIs.
You need to initialize a Firestore instance in order to communicate with the database.
You call the collection method 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 wan’t to save to the database.
You call the set method 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 which are used in cases when you want to write a bunch of data to the database at once.
You call the update method on a document reference to update fields in the document.
You call the delete method on a document reference which deletes the document referred to by the reference.
Adding, updating and deleting operations are asynchronous.
You can use the Firebase console to manage data in the Firestore database.
Where to go from here?
You 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.
Liu vhabp pac’t coqe o jin ju hand xwuta tekzvuuyuquriur. Iq “Vqalgej 34: Sauyokt toxi yyoy Wjoeb Zorakcoda” voi’dw uzm gte ezavend sa gavfuh zuk boge unladov ab zaab pafu. Gau’vy geewd him po keec sude sbil jpu voraleco iyr fa bi octel vezu pakogeteceet inudabioyd.
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.