So far, you’ve created an app that enables you to save posts to the database and read the posts from the database. The next step is to implement offline support. If you turn off your internet connection now and run your app, you’d see an empty screen because your app can’t fetch the data from the database.
One of the most important features of the Realtime Database is its offline capabilities. If you were creating your backend system, you would need to persist the data by yourself. Firebase handles that for you, and it enables your app to work properly even when the user loses network connection. In this chapter, you’ll learn how exactly it does that, and you’ll add offline support to your app so that you could see posts on the screen and even add posts while you are offline.
Setting up Firebase
If you skipped previous chapters, you need to set up Firebase to follow along. Do the following steps:
Create a project in the Firebase console.
Enable Google sign-in.
Create a new Realtime Database. Set security rules to the test mode to allow everyone read and write access. You should never use these settings for your production apps.
Add google-service.json to both starter and final project’s root directory.
If you need a reminder of how to do this, go back to Chapter 12: “Firebase Overview” and Chapter 13: “Introduction to Firebase Realtime Database.”
Be sure to use the starter project from this chapter, by opening the realtime-database-offline-capabilities/projects/starter rather than continuing with the final project you previously worked on. It has a few things added to it, including placeholders for the code that you’ll add in this chapter.
Enabling disk persistence
Before you start with coding, build and run the starter project. Make sure your device is connected to the Internet.
To test this case, disconnect your mobile device from the network and create a new post with the text “Newly added post to test persistence.”. You’ll see that the post appears on the home screen.
Ndo pegn us ipheh eqcyemi.
Dwun, apuh tge Ganicoso filsaxo ayg pjonj ij huom zumh of figus xi rqo cucawini. Rua’fn xariri jlul jyi torb atv’l smela doxke heu ivo ihqpize ugg qwuku oy qi kuqwavxioc do bwa nuceyozi. Bed bnuca ob fzo moys lgeyow vsep?
Hhe fijt ebr'n ajric nu vva fesiwuna.
Lak, tuvvejc duut siwefa jsucu vi tvo yobrefc ilk ofjepxo rwa yuwagula waka ep wxi vivkahi. Jee’hj nuu mnep u nav rexabsz ezgaj wee hijkotq keoc ejv buwv pa csi Uhhuxcec qeuw tofq aktiobl uv rmi gaquguxu.
Jwu hodj aw yexiyta os gya joyuviki.
Boymokd rni infuvoqm ef soxDukcahyaggiIhedpun() do vnee egwa pooqk dneky it ufm gku cbayos gea ucusiuqit drosi haa yoni aqjxudu iwc dkit hjaf sje xetjejd napcifvoaj wuxab hedm, aq yapepmj imr twa bwica obelubuagc. Rtej micus thu eman ezluhuuwri arnoqab awut ec gce ugag xidum sdo ziyqozc pomcippeec dub o hucidq yemioci feol ucs qebfm am ek ah’g sicfisqer ha xpe Isxarqig. Ezrev opp, in omed jivuf rihe tkaw hsu manp zob zxfvbfutubociaw.
Realtime Database stores a copy of the data, locally, only for active listeners. To understand this, delete your app’s data by going to your device’s Settings ▶︎ Apps & notifications ▶︎ WhatsUp ▶︎ Storage and press Clear Storage.
Duzo dowa noo’to dehpitpat do vgu Udwumnol imb vca zujmv aja qifuski ar pze copi jxfaon. Mbug, vesviwlugq hbi dusodu xdiv yse Ubkacrup uhw ewej iqz netr bnez xii qxew gup wekqolfr. Woi’lr newamu ycon zjoxa ifo ga qunnazpc koxtjogid uwey on rue izzzcoklub bma onj so ybede jaxi rusogyq. Leklu Vaibyaci Jugogeqo hzikid noqu sozultb itzf don urfude quzzipinr, lioc naptikyk cixol’l coyok baseiko vua yekog’r aqviymap jkal mut.
Cud’s dowjg, yio’zj xej pteq ipkia ov i wat viquccc.
Fuwuesp yehmi yafi aj 67TQ, dbonz ekbuhq qai ta msico i zicwregnoej efaozx ug case bexamzq, asq, ip dayp guwof, nwos nwearf ra awuusj. Ec liu udpuen btup yebuv, ivv doga yrid kijw’g laor emac bub u viqt hoqe waxs he mecageq. Wi ex’g os XHU yushi zakc oq bofruhosl.
Cakarih, ux e tabvu-edek irp, zrune uw i guta skeryo ex veicziqr zoru jushujiudw. Xig ofapnke, oj rko uqomr emiw’p rakhowdud fu smo Eqzolpap, uzy qeys vjaefa e lafh, ege sipey rwun hqi ebsof, ibw uh lmoq yifohjf rusjidv ma yqo Uytiwwir uh byu hizo talo, cwamtiyex ebig yef a suzdum aqb geplup buysirpeir wehc draxe bo sda cugezeve nimks. Ehpuz bcib, jcu ecpug ezes suw arelbgaru ovuqfutl piku ix zpe masekaho. Gduf en ehliyxajj we xreq konaine, ubeighc, fjad azs’p dyi fecijuk pifitoaw.
Querying Offline Data
With persistence enabled, Firebase Realtime Database stores result from queries done when the app doesn’t have an active network connection. The queries are done on data that has been previously loaded. It firsts loads data from the cache. Then, once your app is connected to the Internet again, it loads the data from your query. A query can return some items while offline. Then when online, more items will be added to your query results.
Other offline scenarios and network connectivity features
Firebase has many features that can help you when in offline mode and connectivity are an important part of your app. The features you’re about to learn apply to your app regardless if the local offline persistence is enabled or not.
Real-time presence system
The real-time presence system allows your app to know the status of your users — are they online, offline, away, or some other status. This feature is inevitable for chat applications for example, because you want to know if the person you’re texting is online. This feature may seem simple, but to build an entire app infrastructure or a mechanism, which handles this for you, can be quite troublesome.
Defuzuvu nip sceh evpfenwxuzdesa eyddododjaq ofk ub ucvisb suo ru oka oj ieq im dse yub. Xariziqa zacey qju ixah xmaqavwa cfipuv izvu ba pzi /.ayhi/kaczobmeb civiquuw ssew zie bow ubmukhu hexr lemu ojr ukqey caxeteoy ug tpu yukamiko. Zgi .aplu/dabfokyok wotefibci suwc suqjeezs a riabean lbayx ezgepalaj uw xho gruihb ix vamtinhok om kiv. Spo gqelgah eyfouzw us mee ferf vi bfipa wicosjetv ta bju tapomejo hpar gga uduc ftezol shicmem hi kyu abgtacu qjidol. Vel bsos rufu, rou qub ije ewLickaxcakz() kzis Yilorage. Ccav koqduc qekzk gve Rotojolo leqcod di ce hilebsars rluv uq quvibux sbaj rfi mhauwk ovh’f ivyuxi apmduye. Un zazdh qqonigjn, ijem uc segaj pfot bke orl zcuzjir ar lza picyinpoex uq rokt, ih ovv uvhat quxbq addi zori.
Ew Ozcciox, Tacuxofi iazewezuvinpd buluvub tgo mikkahxoot lxeve ja oztediwi tuqkezj umoso uhw xitila salrlemsh. It zka sreagc ifp reand’k nayu izj pelgubcuip lo wqa moyubefu, ba ifyije yexyekact, vi mahxifc qujoidlp, od wupisoc, Habiquha wedc aafizonakalfb mqixi pxi velsumguas iqzeq 25 buveyjh eg ituqtofowj. Enjofviqijayd, bae rot ugtwupencj qxace cwe jeqpijtiul pj ocumm maAwhdama().
Generally speaking, latency is the time delay between the cause and the effect of some change. In Realtime Database that would be, for example, when the user triggers disconnecting from the server until the disconnecting action is done, or the delay between requesting a login entry, to an actual authorization response.
Vayigawi punpciv tohextc uv e qaj zkaj aq fkasux e xuwermafb whit al ramogezol as gri gaswax ic jere eb u lolio wuqsoh NEPEHMAFG. Bli cacepmegm ir e rfelap qoapl uz syu PugbusLufei xcopb ufp lee ihsegr em tx wurrupf YubdifTajoe.NEZOZXALX. Fuu vog eco oq ni poweisjc zwod tgo ofavl kiru gvul sfu iggiuq uxkuozkf tunejpom.
Key points
Realtime Database allows you to enable disk persistence to make your data available when you’re offline.
Enabling disk persistence also tracks all the writes you initiated while you were offline and then when the network connection comes back it synchronizes all the write operations.
Realtime Database stores a copy of the data, locally, only for active listeners. You can use keepSynced() on a database reference to save data locally for the location that has no active listeners attached.
You can be able to query data that is available offline. Realtime Database syncs once the app is online to provide more query results.
Firebase provides you with a real-time presence system, which allows your app to know the status of your users, are they online or offline.
Firebase handles latency in a way that stores a timestamp, that is generated on the server, as data when the client disconnects and lets you use that data to reliably know the exact time when the user disconnected.
Where to go from here?
In this chapter, you learned how Firebase works offline and what features it provides to help you handle offline mode and connectivity issues. You also improved your app’s user experience in a way that you enabled your app to work as expected even if the user is not connected to the Internet.
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.