android-room

How to access room db using viewmodel, repository, and livedata

孤街浪徒 提交于 2020-08-26 09:46:12
问题 I am new to android development. In my app, I need to have a user table in the database. I have created the database using room. Now, if I allow to run the db in the main thread, my app is running fine. But, I don't want to run it in the main thread. So, I created the repository and viewmodel classes and calling the db in the main thread using the viewmodel. Still if I don't put allowMainThreadQueries() in the database building statement, java.lang.IllegalStateException: Cannot access

Room using the @Relation annotation - one to many relationship where clause child relationship

拜拜、爱过 提交于 2020-08-25 03:56:06
问题 Using the @Relation annotation. I can query a one to many relationship using the following: @Dao public interface PostDao { @Query("SELECT * FROM post") List<PostWithComments> getPostWithComments(); } Here are the entities @Entity public class Post { @PrimrayKey private int id; private String title; private String content; } @Entity public class Comment { @PrimrayKey private int id; private int post_id; private String content; private String status; } public class PostWithComments { @Embedded