Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet235/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   231   232   233   234   235   236   237   238   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Keeping data in sync
Realtime database stores a copy of the data, locally, only for active listeners. To 
understand this, first, delete your app's data by going to your device's 
Settings 
▶︎
 
Apps & notifications 
▶︎
 WhatsUp 
▶︎
 Stor
age and finally tap on t
he Clear Stor
age 
butt
on.
Next, build and run your app while making sure you're connected to the internet. 
When the home screen opens and posts show up disconnect the device from the 
Internet, once again. Next, open any post that you know has comments. You'll notice 
that there are no comments displayed even if you instructed the app to store data 
locally by setting 
setPersistenceEnabled(true)
. Since Realtime database stores 
data locally only for active listeners your comments weren't saved, because you 
haven't accessed them yet.
If you want to save data locally for the location that has no active listeners attached, 
you can use the 
keepSynced
 method on a database reference, which you'll do in a 
moment.
Saving Data on Android
Chapter 14: Realtime Database Offline Capabilities
raywenderlich.com
234


First, open 
RealtimeDatabaseManager
 class and remove 
private
 modifier from the 
COMMENTS_REFERENCE
 constant.
Now, open 
WhatsUpApplication
 class and call 
keepSynced
 on comments reference 
passing in the 
true
 as an argument:
FirebaseDatabase
.getInstance().apply { 
      setPersistenceEnabled(
true

      getReference(
COMMENTS_REFERENCE
).keepSynced(
true

}
Now, the Realtime database will download the comments and keep them in sync, 
even if there are no active listeners at that location. Whatever happens at this 
location — either data gets deleted or updated, you'll receive an update locally, as 
well.
Connect your mobile device to the network, and build and run your app. When posts 
appear on the home screen disconnect your app from the network. Now open any 
post that you know has comments. You'll see your comments there, this time.
Default cache size is 
10MB
, which allows you to store a substantial amount of data 
locally, and, in most cases, this should be enough. If you exceed that limit, any data 
that hasn't been used for a long time will be deleted. So it's basically an LRU-cache-
kind-of mechanism.
In a multi-user app, if there are two users that are not connected to the internet, and 
both write a post, one later than the other, when they finally connect to the internet, 
they will end up in a race-condition. Whichever user has a better and a faster 
connection will write to the database first. This is important to know because, in 
some cases, this may not be the desired behavior.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   231   232   233   234   235   236   237   238   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish