Android LiveData, multiple tables

Viewed 508

I am using Room. Just like in the Android docs, I have two tables that contain the information I need to display on a recyclerView. They have a one-to-one relationship (I cannot merge them). I've created a POJO, just like in the example below, to contain the merge of both Entities (UserPet).

    @Dao
    public interface MyDao {
       @Query("SELECT user.name AS userName, pet.name AS petName, pet.loved AS loveIt"
              + "FROM user, pet "
              + "WHERE user.id = pet.user_id")
       public LiveData<List<UserPet>> loadUserAndPetNames();

       static class UserPet {
           public String userName;
           public String petName;
           public Boolean loveIt;
       }
    }

On my recyclerView, the cardview has a heart image that can be clicked to check whether a pet is or not "loved". If the user clicks on it, it will trigger an update to the Pets Entity. Unlike the documentation example, my POJO is not within the DAO and my Boolean uses a Converter (just in case you are wondering). I am wondering if the correct way of doing this is adding an insert to the DAO file so the change is propagated. All DAO's I've seen handle a single entity, but I have not seen this documented as a limitation.

@Dao
public interface MyDao {
           @Query("SELECT user.name AS userName, pet.name AS petName, pet.loved AS loveIt"
          + "FROM user, pet "
          + "WHERE user.id = pet.user_id")
   public LiveData<List<UserPet>> loadUserAndPetNames();

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertPets(Pets... pets);

   static class UserPet {
       public String userName;
       public String petName;
       public Boolean loveIt;
   }
}
0 Answers
Related