---
# System prepended metadata

title: Learning Diary 5
tags: [fh, MAD]

---

###### tags: `fh` `MAD`
# Learning Diary 5
## Persistence


Please watch Lesson 6 of the Udacity Course "Developing Android Apps with Kotlin" and implement the example App from the course. Answer the following questions and submit this document as your learning diary 5.

Udacity Course "Developing Android Apps with Kotlin", Lesson 6 (1/2)

### 1. Explain how to create and use databases using the Room library in Android. Include the terms “entity”, “data class”, “query”, “interface”, “DAO”, “annotations”, “class RoomDatabase” and “singleton reference” (approx. 200 words).
(1 point)
To create a DB the following architecture is used:
![](https://i.imgur.com/fhzG3Tb.png)

Every single entry inside a DB is called an entity. In the programm every entity is a instance of a class (also called "data class") that holds its information. The bridge between the entitys inside the DB and the App is the DAO (Data Application Object)class which holds functions which then again hole querys to interface with the DB.
To make the whole system more easily understandable and also reference for example to which specific colomn specific data is refering, annotations are used to give each function, class and value a "Tag".
To create a Database for your own app the Room library is provided so that your own Database may inherit from the RoomDatabase class. There should only be one instance of it (i can't think of a example where you would need 2 instances of the same DataBase). Therefore you implement it with a singelton pattern.


### 2. Explain how multi-threading work in Kotlin/Android. Include the terms “callbacks”, “coroutines”, “suspend function”, “job”, “dispatcher” and “scope” (approx. 200 words). Please also show example code snippets using coroutines different from the one given in the course.
(1 point)
Multithreading in general is the process of working multiple jobs at "once" by running them sequencial on one or more processors. If that happens fast enough it seems to the user as if they all were processed at the same time -> in parallel. The thing that stands out in Andoid/Kotlin is that there is a Main-Thread or the UI thread which handles what the user can see.
* **Callbacks** start long running tasks on a background thread but cant use exceptions.
* **Coroutines** handle long running tasks as well but do it more efficient and can handle exceptions.
* **suspend function** while a big job is being done the process continues until the job is done and its results can be used.
![](https://i.imgur.com/S2yW73E.png)

* **job** anything that can be canceled. It has a lifecycle at whichs end it returns something.
* **dispatcher** sends of coroutines to run on various threads.
* **scope** combines information including a job and dispatcher, to define the context in which the coroutine runs. They keep track of couroutines.


### 3. Start with the starter code for Sleep Quality Tracker App and implement coroutines as described in the course. Please reflect on your key findings, learnings and problems with the implementation of the app (500-1.000 words), particularly focusing on using Room and coroutines. Include 4-7 screenshots of the most important implementation steps from your point of view.
(3 points)

Starting off i noticed that the project was downloaded locally this time which i didn't like since i very much enjoyed the TODOs, which werent realy there now since i could not go to the step that i am atempting throught version control.
I later found out how to still get to the repository and clone it.

#### SQLite Primer
Also, and that was quite pleasant, everything in the short descritpion of the "SQLite Primer" section was already known to me due to this years "Datenbanken" Lecture.

#### Creating the SleepNight Entity
The Part about constructing the data classes was interesting. I already knew how to consturct something like that since we covered that topic in our second semesters Programming2 Lecture BUT the Anotations in kotlin make it a lot easier to understand in my opinion.
![](https://i.imgur.com/EE1bWvF.png)

#### Data Access Object (DAO)
creating the functions with the corresponding SQL statements is qite simple and if my memory serves right a lot easier than in java but i might be mistaken.
![](https://i.imgur.com/ypYRQET.png)

####  Creating a Room Database
This lesson was a bit hard to understand but after the second time watching the video i think i got what he was trying to say. I personally would be interested in finding out how to make the database peristant throughout different versions of the database instead of using the ".fallbackToDestructiveMigration"
![](https://i.imgur.com/QhQf96K.png)

The handling of threads was quite a bit to take and and understand but i think i got the hang of it. Where i noticed that i understood it a bit better was the process of implementing the couroutines where you allways first defined the function and the another one to reference the DAO methods i had previously defined.
![](https://i.imgur.com/UBOZU5y.png)

![](https://i.imgur.com/kWPjFaS.png)

What was realy awesome about this exercise is seeing how parts from previous semesters like threading and sceduling from operating systems from the first semester, or databases in programms via DAO's from second semesters "Programmierung2", come together.



Open Questions (2/2)
Please list any open questions.

