Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet272/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   268   269   270   271   272   273   274   275   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Listening for comments
You can add comments to the database now, but you still can't read them. Since 
comments are stored in a separate collection from posts, to read them you'll need to 
write a query that returns comments for the specific post. Every comment document 
has a 
post_id
 property that indicates to which post the comment belongs to.
Open 
CloudFirestoreManager.kt
. Add a 
commentsRegistration
 field:
private
 
lateinit
 
var
 commentsRegistration: ListenerRegistration
You'll use this field to assign the comments listener to it and to remove the listener 
when needed, just like before.
Next, navigate to 
listenForPostCommentsValueChanges()
. Replace the 
TODO
 
inside the function with the following:
// 1
 
commentsRegistration = database.collection(COMMENTS_COLLECTION) 
    
// 2
 
    .whereEqualTo(POST_ID, postId) 
// 3
 
    
// 4
 
    .addSnapshotListener(EventListener { value, 
error -> 
      
if
 (error != 
null
 || value == 
null
) { 
        
return
@EventListener
 
      } 
 
      
if
 (value.isEmpty) { 
        postsValues.postValue(emptyList()) 
      } 
else
 { 
        
val
 comments = ArrayList() 
        
for
 (doc 
in
 value) { 
          
val
 comment = doc.toObject(Comment::
class
.
java

          comments.add(comment) 
        } 
        commentsValues.postValue(comments) 
      } 
    })
Saving Data on Android
Chapter 18: Reading Data from Cloud Firestore
raywenderlich.com
267


1. As before, you assing a listener to the 
comments
 reference.
2. You use 
whereEqualTo
 method to create the query that filters the documents in 
the collection that contain the specified field and value in that field.
3. Pass in the 
POST_ID
 that represents the 
post_id
 property and the 
postId
 that 
represents the value for comparison. This query will only return the documents 
that belong to the specified post. 
whereEqualTo
 method returns a 
Query
 that you 
can read or listen to.
4. Once again, attach an 
EventListener
 and parse the 
Comment
s, if there are any, 
updating the UI when you're done.
Next, open 

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   268   269   270   271   272   273   274   275   ...   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