Saving Data on Android



Download 19,28 Mb.
Pdf ko'rish
bet227/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   223   224   225   226   227   228   229   230   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Saving Data on Android
Chapter 13: Reading to & Writing from Realtime Database
raywenderlich.com
226


Query
 class is used for reading data and it has many useful methods that allow 
you to fetch the data in a way you want. You can filter data by some criteria, sort 
data, limit, etc. Check the official documentation (
https://firebase.google.com/
docs/reference/android/com/google/firebase/database/Query
) for the 
Query
 class 
to see what it offers.
2. The 
equalTo
 method returns a 
Query
 instance which contains child nodes only 
where the node value is equal to the specified function argument. In this case, it 
will return a query with the comments for the specific post.
The rest of the code is the same as the code for listening for post updates.
Next, add a  
deletePostComments
 function which will delete all of the comments for 
the specific post:
private
 
fun
 
deletePostComments
(postId: String)
 { 
    database.getReference(COMMENTS_REFERENCE) 
        .orderByChild(COMMENT_POST_ID_PATH) 
        .equalTo(postId) 
        .addListenerForSingleValueEvent(
object
 : 
ValueEventListener { 
          
override
 
fun
 
onCancelled
(databaseError: DatabaseError)
 

            
/* No op */
 
          } 
 
          
override
 
fun
 
onDataChange
(dataSnapshot: DataSnapshot)
 

            dataSnapshot.children.forEach { it.ref.removeValue() 

          } 
        }) 
}
It uses exactly the same logic for fetching the comments for the specific post as the 
listenForPostCommentsValueChanges
 function and you're already familiar with 
how to delete data from the database. Call this function from the 
deletePost
 
function passing in the key of the post. This makes sure that when a post gets 
deleted, its comments get deleted as well.
Next, add an  
onCommentsValuesChange
 function which starts listening for 
comments updates and returns a  
LiveData
 object:
fun
 
onCommentsValuesChange
(postId: String)

LiveData> { 
    listenForPostCommentsValueChanges(postId) 
    
return
 commentsValues 
}
Saving Data on Android
Chapter 13: Reading to & Writing from Realtime Database
raywenderlich.com
227


Now, open the 
PostDetailsActivity
 class again, navigate to the 
listenForComments
 function and replace its TODO comment with the following:
realtimeDatabaseManager.onCommentsValuesChange(post.id) 
  .observe(
this
, Observer(::onCommentsUpdate))
This just starts listening for the comments update. Note the 
Observer
 in this case 
requires you to import  
androidx.lifecycle.Observer
.
Next, back in the 
RealtimeDatabaseManager
 class, add a function for removing the 
comments listener:
fun
 
removeCommentsValuesChangesListener
()
 { 
    
database.getReference(COMMENTS_REFERENCE).removeEventListener(co
mmentsValueEventListener) 
}
In  
PostDetailsActivity
, override 
onStop
 and call 
realtimeDatabaseManager.removeCommentsValuesChangesListener()
 to remove 
the comments listener when you no longer want to listen for comment updates.
override
 
fun
 
onStop
()
 { 
    
super
.onStop() 
    
realtimeDatabaseManager.removeCommentsValuesChangesListener() 
}
Build and run your app. Navigate to the same post you added a comment to, earlier.
Saving Data on Android
Chapter 13: Reading to & Writing from Realtime Database
raywenderlich.com
228


Now you can see your comment on the UI as well. Add more comments from the 
same and from the different account to see how comments are updated in real time.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   223   224   225   226   227   228   229   230   ...   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