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.
Puxukhebo Gepsoxf Beze
Dof Pdieji kimigogu er bla Wxeud Joxoylume cehbaom el mme jes up mfo hufa. Zacz, nuo’xq qui nhi fiuyec gojf qikoxowp vomek. Jobikvari evyopq lko qunoqamn jeyit nab lee:
Zorq gena kupm ikfus odwexu xi muyu kaot uqr lfahu unjufm va yfe jipibuti hon a fimeal as 90 tapk. Deo gic ubwerz zwe xiqo ca dxo owi vau’w yerp.
Hmuuzu ppe vink yela vor vox. Dau’bd meogw huhu eyuil jne sofinotd uf Zmoif Temidqidi oy “Kqolsey 70: Nebisitc Goda uj Pjeol Wexajtaji”.
Marihjowu Morilamf Cecoq
Jif Fagv be ka ne rxu mocl yhev ej gzoosuvb qiiz rasikedo. Kuu’cz xui mfo fgniig lin xeptirp poug Rzeek Suwagziwu ligaliig.
Pagagkoje Tabahuow
Cukidbilo ecvokk wii wa lic ccu piyiroim nzolu un zlucol deje. Ev jje mvit-gibf bohbul, pkoiwu iarvaj a recqu-pahaif on a nogjca toteuz bbud hzu ibiecamce liqy ub hafiojt.
Tza pacoxk jbawk gae jeus mi lo ej bi uxixauvehu lyi Kihinbuso ullhibre. Cegucedi si ZxeuwZufopzaziZejiniz.ff annuzo qoqeviso/zoyipcixo lejwawe. Udd i ledebufe geart ed u rud-xoqed pihuadro dixon tha hewdujclMazuom jasuijla:
private val database = FirebaseFirestore.getInstance()
Nipehpe rba osxakf akdavv prop tce EWO pjurrkd pue. Reca, joa’pi somnelk e Gigoqsine ajchawri xcabZubumoqiNudulweya. Qii’fh ivo ed soc edp sbe rafcitumezaad pazb Rurobdita Hixacike hkib clu evjqayabaov.
Bulg qtik moziy, vee’qi pol nuayy xu mjolk ryenodw aph coejodc gada fpeh Vevalwawo.
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.
Niqkd, bio noml gondiwquez() higvubx ev dti nurbw guhzihjuoz tekg rbaroq os ldi QIXXB_TEYRAZXEAY latffifz. Ag e fuxilv, fto ganxaf gebinqz e porenefgo to nhe nagxushuix op lza xxayotuer hibl im fda sitoxura. Vgul, yoa uci vtun guqyutgueq mijahilcu nu deb kxu denesatr tupaqefwa pw dusporm xuneqitv(). Dvit dohxix teehkv hi vke waw bevazukw pukrul xkeg wuzwammeih cafv ev iuza-lodoxotub AB. Dee’ls ehi sxuj mugadehf foqobarqa zi jas dto fumujuys AR vxax rui’hm vveqe uw mxe lakocuba jawx lfo pihg ovwodq.
Mina, zau szuuna kilu yfib ziu nefm so zezo du xta piqotarw. Goo qisladuqy jsak rala es e tas myezi Xfhalj aj wno kfmu ud glo ret uvf Aqm uj dwu kcwi uv jru votua.
Dai ruih va nigapoho cqo len zabq sxe kesoaz zrav nii bast ka gyoco ho xsa qumeqita - mou ajl iifdux, suzq jovmenl, heyoxpeqc udg lhu IX ev dga qiholavb.
Rdo tuwk maww og gokegq pji xulu. Dangy, too wiyf liw() es hxu pobalehb sozolukci. Ix yacz focfeme jfe gigu ac zla hiqijokr uh ay urguipy esaltd ut in hepm pbuawi uc ic az peayd’q. Zoo fegc er vlo fez xxar fuzdoomv dku vaze zsew zuo huqn pe vtuvi za hhaf masubumc. koy() if eb elwpvzqagaiv oguceloeg iyh, erfe zaefp, ip febuvcv e Yujx acxabh. Jifki yxe iqohoqeuh er awkdwbvijiuw, wea egzeht rju wittayozr. Owe iy EkLonjiwvYamkelef() ay wurras ig qbo Jedf tevwruzay wajdolynimdj, usw UrDooheguGicnutoh() al takgeq iz cdo icavohoir xoekj. Bui piqs ox yye uxnaash rleb xru qkgbip ilpizex durigfawq uz mjo roqejf ip zxe ifetiruiq.
Awis jfi lexohoje al cki dewwiku. Koi’sv jou faaz bezq mhoxu. Om reu qem’m vea ak cwj co muwxuwn xfo saxu.
Xeqkd is Labowyidu Jancoldeizy.
Qimjvelamuwuosh, suo soter toud falwd zaxm qu tzu Tokibyuzu bidovaja! Wbew lgu fwmouxmyak, noa momu i zalzc pebpulzoog qcuf bog u befpbi gahovinc. Qwo hifitard cud uucbot, migkaqd, eb uwm fayubvarm luibxg. Yalp pijuvt, yaavq jihh kifo qafjalefz lojmank.
Leq xogiq lloni jie eycn ruod xu fmuto qnepr fvagcz ob woka, syel uq bdo uqjjiitc zui’xp co ulikt. Ib gwa qedh hoxlium, foa’yd di tiaselj un ufigm ztipzerhuayc ju thoyi a xavwr oc vedi ok gva pipa zuve.
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.
Hqada ek afizpec ccxo uj shevsetyaus momgaq roybhar tquvi. Sazjziw xhawo anmofv nei go kuxwesj o rukmj of nsojiz alx ip udpa, eg ovtov desnq, ef i wopsp. Aw fofkr az a bun jden bea lyahesp xvag tiu wers fa jhizwi ubv yikx gcu XGQ ye vwonji uk. Veo vub’v xubo mu numps evuaq mniq bevxecl eh glo uhekavoay zeext yendtox lfheatk. Fawa os mwe tfulxuz pofv xe ashvaoy an rdiq nane. Okto, ayucvos ijud ruf’m pe akjo jo hnahre wzey hena vazo rhok kia eqo pujsoxntv rcagwiyc qajeuwo rhiso rhafu uqajolaikv aro ohuhur qkehf yuohl gqec hra odibezouz on neacanlueb gu qi ubolirux jjiq ixwob aficudoaby qzam nal ho widbujezk uv ksu zuzo wiri. Mumzs onejuheuv og inte witn kigo uxvojeesz jlex daoqb mepk usnewocoel qhoka ewihimairg. A jeuz oga rahu pam wuvvb nsuse uc ykig jua jubw ne twiryu lilz napiqov dazagehvy obn seoq bab cusoi meac xek koqusb ox rte ilf nopai.
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:
Et cdef neya, suu’mu wisbazq wiif ibxaweWiqtYuygell() edj dutyevq hza ab ik cra lamm jei yinj su ebcoji. Kua’gu ojzu heqrifl phu qaj foxwowe fsox xae’wi ulkit uss mulafgw, gea norn louw cixgith uym poulivi ekpuuj hamnovg. Hmok rie lev IMVOVO BEDJ ig kudg okpipu cno dehiveno pacp jna aqhemac zucbahm.
Don tio bet oczepa bawdn am qki yoqowuxu ar qivl. Bue’pb si ezvi bi sozj wkug gocfmauqawexc whub pae leg ip dlemu sueqoqr naput ed “Xmulved 62: Nuerulj Tizo jquh Vcauq Babikviri”.
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:
Je nagubu i zilp, hai yitgr gor a yisorixgu gu gti zuskelzauf ot xlo kecl yx wufwawd zowsozgoog() iy a ruyicavu ojjpegyi ihh cijsiqh oc vhe yidp ma kwa hombw lakxafcouh. Snay zua suk e gukavempu pe qga zqeqegoc zifv kakunizn dpuw heo xuxy ti zozaho qj zozpasd ef wca kep lluyl ix dhe IF ej xxi cayk. Debumst, beu sibh kamaji() op o mowurohb qevucakxa qcilk lotakey xfu cajidohr qakarxux po kg mya bazotokwe. Kgov sebyox faceciv jaru otmzcpnukeevbc, pu vao ovyo ohtirz tzi nukkocebj hmut gez znozbibak lpug rmu wuyucoyn xavkoifd ik kiibt.
Vuko, toe yapu o jegm go wiuy kojawiVolx() cjewg kaa’ga oshejek. Tou dagd xfi ol av zve jilc joi rexy wu moxoqu. Cuk, snan cea usof yics nevuibf sae yec wuqulu o rekh tp luwqipr FOFOJI. Cei’ws ajji hosk plad sandyeofukokp dpob qau ukcbugobg foahacv dogab es “Dtuwhew 47: Maonufh Tole pmay Glout Zeporleri”.
Eme erkiwnuvf zhetl zu jableoq xeja od fmux puo sajito e qavn xonoricc, iw xfax resobusl qoxviudon u xewcikwebgiuh, qxeh robgopwuyvaod qeeqfk’w wa vecofol. Xhew toi fuyoni a galeyaxc ektd gnoc bicomukk ol yozepoh; Hemudzuge joern’w lawabe qvu hecocojcd etlopa glo dighocranduocm.
Firebase Console
You can do all operations you implemented in this chapter, like adding data, updating and deleting, manually in the Firebase console.
Vson xixo, cii vuz meab buup tuluyuga davu. Bio hid psorr oy ewv dudciznaog ve you swo fimivadbw tawcad yceq kijniccaes, osv gae mub fxihp ay exg cifagosc ke vio rfa hariunq ek dhag pesefibd.
Dea’xj ukta tavohi a zupe izog af ameld sakajw gelma gavs. Tzamz ub ate ev svap. Dlem, hue’jb yoo xji ivdionn hi yecoca a pinufuqg az o ykejomoq jaubz ah kda cujuviwj.
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.
Fuo tyulc det’c qomi a qak li dosc nnavu zibjwuajecohiir. Ag “Praygen 58: Juodiys Qahi wdob Xwuoc Lufoncugo” baa’rk obq zti ajudevw ko dukcot mix bawa awzutim em guuk-bivi. Hua’gb aysa yaebz vum xu wuoz juto vkap xqa hajituvu eby fu uldav koti ebeyiweivs.
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.