. I want to retrieve its value in repository class to determine whether to like or unlike a post. I'm using below code but app automatically crash with
I/Process: Sending signal. PID: 12911 SIG: 9
when using below code block. what am i doing wrong?
Copy code
@Query("SELECT * FROM posts WHERE postId = :postId")
fun isPostLiked(postId: Long): Post
@Query("SELECT * FROM posts WHERE postId = :postId LIMIT 1")
r
rajesh
08/16/2021, 12:31 PM
not working
rajesh
08/16/2021, 12:31 PM
can i use retrieved room entity in repository to decide further logic? or i must have to use it on UI thread?
i
Ian Lake
08/16/2021, 1:40 PM
Make your method a
suspend fun
and it'll run on the right background thread automatically
r
rajesh
08/16/2021, 1:56 PM
Hi @Ian Lake, tried that one but still app crash
Copy code
@Query("SELECT * FROM posts WHERE postId = :postId LIMIT 1")
suspend fun isPostLiked(postId: Long): Post
suspend fun toggleLike(postId: Long) {
Log.d(TAG, "this will be printed")
db.postDao.isPostLiked(postId).isLiked?.let {
if (it) {
Log.d(TAG, "post already liked, its not working")
} else {
Log.d(TAG, "post not liked, its not working")
}
}
}
rajesh
08/16/2021, 2:05 PM
Even, update query is not updating db
Copy code
@Query("UPDATE posts SET isLiked = 1, likesCount = likesCount + 1 WHERE postId = :postId")
suspend fun likePost(postId: Long)
rajesh
08/16/2021, 2:27 PM
@Ian Lake its working now, thanks. reason of app crash was, item was not found in db.