JungIn Choi
04/07/2021, 1:07 AMDay25
• Review Lesson6, revisiting the details
◦ synchronized block - only one thread of execution at a time can enter the block
◦ smart cast - (link) Compiler infers the variable’s type and internally casts it. It allows access to the nullable variable after checking it’s actually not null
◦ Testing & annotations @ Before , @ Test , @ After
◦ <_merge_> tag & reusing layout (link)
◦ requireNotNull - throws an exception if a value is null
◦ Creating ViewModel
▪︎ needs application, since ViewModel has to use resources(string/style ..)
▪︎ needs DAO, since ViewModel has to access the database through DAO interface
▪︎ needs Factory to instantiate the ViewModel - Reference to the “database” is not needed, since there is DAO interface passed. However, existence of database should be checked when the ViewModel is created.
▪︎ Inside the Fragment/onCreateView(), create instance of Factory, get reference to the ViewModel, set data binding for ViewModel
• Conceptual basics about android thread, process, runnable (blog post)
• Lesson7
◦ RecyclerView, Adapter, ViewHolder
▪︎ ViewHolder - holds views, store information for RecyclerView, works as RecyclerView’s main interface
▪︎ Adapter methods - getItemCount, onBindViewHolder
Self-Comment
• Proceeding the lesson is important, but reviewing is also important. It’s a difficult mission to maintain the balance, since if I spend too much time and energy on reviewing, I can’t proceed. But if I just proceed with unsolved understandings, it’s quite useless.
• Curious about android testing, testing methods, specific use cases, flexible and wise usage of various testings in actual industrial deployment.
• Faced this bug Test running failed: No test results. onError: commandError=true message=INSTRUMENTATION_FAILED:
while running the provided test code. Seemed to be just another build..gradle..version..kind of problem, so I skipped this at that moment, but I’ll have to resolve this now while reviewing..
Goals Tomorrow
• Finish reviewing lesson6, proceed more on lesson7@Volatile
private var INSTANCE: SleepDatabase? = null
fun getInstance(context: Context): SleepDatabase{
synchronized(this){
var instance = INSTANCE
// code for null checking & creating database
return instance
}
}
In the lecture, the lecturer mentioned something about smart cast. But at that time, I did not know what smart cast is, and which part of this code is showing this smart cast feature. It did not seem to be the core part of this lesson (which is actually true), so I just passed away and proceed the lecture. So, I checked what smart cast is, and now I understand.
INSTANCE which is declared as a nullable variable, but not null in actual usage, can be returned in getInstance function which returns a not-null SleepDatabase type variable.Bryan L
04/07/2021, 1:11 AMcompanion object {
@Volatile
private var INSTANCE: NoteDatabase? = null
fun getDatabase(
context: Context,
scope: CoroutineScope,
) : NoteDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
NoteDatabase::class.java,
"note_database"
)
.addCallback(NoteDatabaseCallback(scope))
.build()
INSTANCE = instance
instance
}
Here is a code sample from my Note database you can see for another example that includes the filler code( // code for null checking and creating database )JungIn Choi
04/07/2021, 1:15 AMBryan L
04/07/2021, 1:22 AMval x: Int? = null
val number = x ?: 5
println(x)
output = 5
So it get's the value(If not null) on the left of ?:
else if left value is null than use this value instead.
Helps control the flow of null values if they are a possibility.
In this case. If an instance of the database already exists, return it. If not(it's null), create an instance of the databaseJungIn Choi
04/07/2021, 1:29 AM