Problems covered in this chapter |
|
In this chapter we deal with the class CrudDiaryDatabase.kt, which creates the database and allows us to access the database in Kotlin, a converter class that ensures that date values are stored as long values in the database, as well as the XML file string.xml, in which we store English (main language) and German strings and which we use when pre-filling the database.
Most of the code in this class is so-called boiler-plate code and looks quite similar whenever you access an SQLite database with Room. Interesting is the method onCreate() specified in addCallback(), in which we do the pre-filling of the database when it is first created. It is important here that the insert statements for the initial values are executed in a single transaction. With a few values this is not so important, but with a large number of insert statements, such as in the GeDaMa app, the start-up time of the app increases dramatically if these statements are executed as separate transactions.
Note: If this start-up time (on older devices) exceeds the 10 second mark, you will receive a corresponding warning in the app store!
Therefore, pack all insert statements as in the code above into the separate runnable class DatabaseInitialization and execute its run method with the method runInTransaction() in a single transaction. Because the database values to be entered are determined beforehand using context.resources.getString(R.string.xxx), you can fill the database with the appropriate, language-specific values depending on the user’s language setting: If the user has set German as the default language, for example, the genders table is filled with the German values männlich, weiblich and divers; if the user has set another language, the table is created with the (default) English values male, female and diverse.