2

I'm working on dictionnary application in which the api request have many nested lists , i have tried to insert all the nested lists but i'm getting different error each time , i would like to know what is the best way to save multiple nested lists , should i use room relations or something else , thank you in advance for help , i m really stuck with this for few days now

  • This is sample schema of how are the lists nested

enter image description here

  • This is the parent list
@Entity(tableName = "DICTIONNARYTABLE")
@TypeConverters(DictionnaryModelConverter::class)
class DictionnaryModel : ArrayList<DictionnaryModelItem>() {
   
   @PrimaryKey(autoGenerate = true)
   @NotNull
   val wordId: Long = 0

}
  • The parent list has two lists as well
@Entity
data class DictionnaryModelItem(

   @PrimaryKey val dictionnaryModelId: Long = 0,

   @TypeConverters(DictionnaryMeaningConverter::class)
   val meanings: MutableList<Meaning>,

   @TypeConverters(DictionnaryPhoneticsConverter::class)
   val phonetics: MutableList<Phonetic>,
   val word: String

)
//---------------------------

@Entity
data class Meaning(
   @PrimaryKey val meaningId: Long = 0,

   @TypeConverters(DictionnaryDefinitionConverter::class)
   val definitions: List<Definition>,
   val partOfSpeech: String
)
///-------------------------------
@Entity
data class Phonetic(
   @PrimaryKey val phoneticId: Long = 0,
   val audio: String,
   val text: String
)
  • inside the meaning , i also have definition which another model
@Entity
data class Definition(
   @PrimaryKey val definitionId: Long = 0,
   val definition: String,
   val example: String,
   @TypeConverters(DictionnarySynonymsConverter::class)
   val synonyms: List<String>
)
Taki
  • 3,290
  • 1
  • 16
  • 41

1 Answers1

3

You need to create one-to-many relationship data model here. For instance each dictionary word has many meanings and many phonetics. Here Dictionary is a parent entity and Meaning and Phonetic are the child entities. Each child entity will have it's parent entity primary key stored in its table. You will need another data class to define this relationship.

data class DictionaryWithMeanings(
    @Embedded val dictionary: Dictionary,
    @Relation(
          parentColumn = "dictionaryModelId",
          entityColumn = "dictionaryId"
    )
    val meanings: List<Meaning>
)

Meaning table has to store dictionaryId as foreign key its table. Same has to be defined for phonetics. And Meaning table again has similar relationship with Definition and so on.

Varsha Kulkarni
  • 1,389
  • 2
  • 13
  • 17
  • Thank you for your reply , i have edited my code above because i missed to add some code for another model class , cause meaning itself contain another model , can you please recheck the code again and tell me if one-to-many is still good option to go with , thank you in advance for help – Taki Feb 20 '21 at 00:21
  • should i create dictionnaywithmeanings and then seperately created dictionnary with phonectics then again meaningwithdefinition ? – Taki Feb 20 '21 at 00:22
  • Please clarify what is the purpose of DictionaryModel table here. – Varsha Kulkarni Feb 20 '21 at 00:34
  • i used plugin in android studio to generate pojo for me , so the Dictionnarymodel is the first entity which actually contains list of DictionnaryModelItem , then the DictionnaryModelitem its self contain list of meanings and list of phonetics and last meanins has a list of definitions – Taki Feb 20 '21 at 00:40
  • this is sample of the api https://pastebin.com/ESWy4nG7 – Taki Feb 20 '21 at 00:43
  • AFAIK you can go ahead with one to many relations for these, you need to have Word, Meaning, Phonetic, Definition tables and so on if there is deeper nesting. – Varsha Kulkarni Feb 20 '21 at 01:10
  • so i guess what i should do is to create data class for each two entities , DictionaryWithMeanings , then i create another for DictionaryWithPhonetics and so on right ? – Taki Feb 20 '21 at 01:16
  • Right! Read more here: https://developer.android.com/training/data-storage/room/relationships – Varsha Kulkarni Feb 20 '21 at 01:31
  • i have defined relationships for all but the issue is as for example in the developer site example they are passing fun getUsersWithPlaylists(): List, whereas in my case i m defining dictionnarywithmeanings , dictionnarywithphonectis , dictionnary with definitions , how would i observe all these ? it is confusing me little – Taki Feb 20 '21 at 01:58
  • Thank you for your help and time , i fixed it – Taki Feb 20 '21 at 03:03
  • I haven't used nested one-to-many relationship yet. To query and construct the DictionaryWord by running operations has to be done in a single transaction by annotating with `@Transaction` – Varsha Kulkarni Feb 20 '21 at 03:56