Saving Data on Android



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

PostDetailsActivity.kt
 class and override 
onStop()
. Remove the comments 
listener from this method because this is the point where you're no longer interested 
in the comments changes:
override
 
fun
 
onStop
()
 { 
    
super
.onStop() 
    cloudFirestoreManager.stopListeningForCommentsChanges() 
}
If you build and run the code now, you should see the comment appear, in the post 
details section of the app.
Saving Data on Android
Chapter 18: Reading Data from Cloud Firestore
raywenderlich.com
268


Dele/ng comments
One last thing that you need to add is the ability to delete the comments. You'll 
delete the comments for the particular posts when that post is deleted.
Open 
CloudFirestoreManager.kt
 and navigate to 
deletePostComments()
. Replace 
the 
TODO
 inside the function with the following:
// 1
 
database.collection(COMMENTS_COLLECTION) 
    .whereEqualTo(POST_ID, postId) 
    
//2
 
    .
get
() 
    
//3
 
    .continueWith { task -> task.result?.documents?.forEach 
{ it.reference.delete() } }
1. First, get a reference to the comments collection and filter the comments that 
belong to the specific post.
2. Call 
get()
 to retrieve the filtered-by-post comments, 
asynchronously
.
Saving Data on Android
Chapter 18: Reading Data from Cloud Firestore
raywenderlich.com
269


3. After comments are loaded, delete them from the database, one by one.
continueWith()
 on a 
Task
 instance returns a new 
Task
 that will be completed with 
the 
result
 of applying the specified 
Continuation
 to this 
Task
. A 
Continuation
 is 
a function that is called to continue execution after completion of a 
Task
. When the 
comments for the specific posts are fetched, you delete them by traversing the result 
documents and calling 
delete
 method on each document reference. // TODO FPE - 
Should we try to separate and clarify this more? And how?
Finally, call 
deletePostComments()
 from 
deletePost()
, to delete the comments 
tied to that post, when thte post is deleted:
fun
 
deletePost
(key: String, onSuccessAction: ()
 -> 
Unit

onFailureAction: () -> 
Unit
) { 
    ... 
    deletePostComments(key) 
}
Build and run your app. Open the post that you added a comment to before. You'll 
now see that your comment is displayed, like before. Add another comment and 
you'll see that it's displayed on the screen immediately.
Saving Data on Android
Chapter 18: Reading Data from Cloud Firestore
raywenderlich.com
270


Open the database console. Now, delete the posts that you added comments to and 
observe the database in the console. You'll notice that comments for that post are 
deleted, as well.

Download 19,28 Mb.

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