Android Room: Insert relation entities using Room

前端 未结 6 1603
鱼传尺愫
鱼传尺愫 2020-11-27 10:41

I\'ve added one to many relationship in Room using Relation. I referred to this post to write the following code for relation in Room.

The post tells how to read th

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 11:16

    Currently there is no native solution to this problem. I have created this https://issuetracker.google.com/issues/62848977 on Google's issue tracker and the Architecture Components Team said they will adding a native solution in or after v1.0 of Room library.

    Temporary Workaround:

    Meanwhile you can use the solution mentioned by tknell.

    public void insertPetsForUser(User user, List pets){
    
        for(Pet pet : pets){
            pet.setUserId(user.getId());
        }
    
        petDao.insertAll(pets);
    }
    

提交回复
热议问题