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.
Do'stlaringiz bilan baham: