Saving Data on Android


Listening for data changes



Download 19,28 Mb.
Pdf ko'rish
bet267/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   263   264   265   266   267   268   269   270   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Listening for data changes
If you don't have the project open by now, make sure to open it, and head over to 
CloudFirestoreManager.kt
. Add a 
postsRegistration
 field like this:
private
 
lateinit
 
var
 postsRegistration: ListenerRegistration
Saving Data on Android
Chapter 18: Reading Data from Cloud Firestore
raywenderlich.com
261


ListenerRegistration
 represents a subscription of sorts, for a database reference, 
when you attach a listener to the reference. You'll use it to assign a 
posts
 listener to 
it and to remove it when needed, to clean up your code, and to stop receiving 
changes.
Next, navigate to 
listenForPostsValueChanges()
. Replace the 
TODO
 inside the 
fuction with the following implementation:
private
 
fun
 
listenForPostsValueChanges
()
 { 
  
// 1
 
  postsRegistration = database.collection(POSTS_COLLECTION) 
// 2
 
    
// 3
 
    .addSnapshotListener(EventListener { value, 
error -> 
      
// 4
 
      
if
 (error != 
null
 || value == 
null
) { 
        
return
@EventListener
 
      } 
 
      
// 5
 
      
if
 (value.isEmpty) { 
        
// 6
 
        postsValues.postValue(emptyList()) 
      } 
else
 { 
        
// 7
 
        
val
 posts = ArrayList
() 
        
// 8
 
        
for
 (doc 
in
 value) { 
          
// 9
 
          
val
 post = doc.toObject(Post::
class
.
java

          posts.add(post) 
        } 
        
// 10
 
        postsValues.postValue(posts) 
      } 
    }) 
}
1. You assign the listener to 
postsRegistration
, by attaching an 
EventListener
 
to the database collection.
2. You receive the database collection by calling 
database.collection(POSTS_COLLECTION)
.
3. You call 
addSnapshotListener()
 on the collection reference which starts 
listening for the data changes at its location.
4. When 
onEvent()
 is called you first check if an error occurred by checking if the 
error argument 
is not
 
null
, or if the value 
is
 
null
. If it is you'll just return from
Saving Data on Android
Chapter 18: Reading Data from Cloud Firestore
raywenderlich.com
262


the function, since you cannot consume the event.
5. Check if the received data actually contains documents - it isn't empty.
6. If the value is empty, it means no posts are available, and you simply 
communicate it by updating the 
postsValues
 with an 
emptyList()
.
7. If there are values, instantiate an 
ArrayList
 to store them.
8. Iterate through each document in the value snapshot.
9. Parse each document as a 
Post
, and add it to 
posts
.
10. Update the 
postsValues
 with parsed posts.
To read the data, you pass in the listener of type 
EventListener
 that will be called 
whenever data changes or if an error occurs. 
EventListener
 is a generic interface 
that is used for any type of event listening, and contains only the 
onEvent(T value, 
FirebaseFirestoreException error)
 function that you're required to implement. 
onEvent()
 will be called with the new value or the error if an error occurred. You get 
the result data as a 
QuerySnapshot
 object. This is the easiest way to communicate 
data changes or errors, and since it's 
generic
, it can work for any collection.
Open 
HomeActivity
 and navigate to the 
listenForPostsUpdates
 function. Replace 
the 
TODO
 inside the function with the following:
private
 
fun
 
listenForPostsUpdates
()
 { 
  cloudFirestoreManager.onPostsValuesChange() 
      .observe(
this
, Observer(::onPostsUpdate)) 
}
When new data is received you'll reflect that, by displaying it on the screen.
You also need to remove the listener when you no longer want to receive data 
change events. To do that, open 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   263   264   265   266   267   268   269   270   ...   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