Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet110/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   106   107   108   109   110   111   112   113   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

124
CHAPTER 8: APIs
The construct to achieve this for a method inside a DAO class looks like this:
@Query("SELECT * FROM employee")
fun getAllSync(): LiveData>
So, you basically wrap a LiveData class around the result, and this is what you can do with 
all your queries.
However, this is possible only if you add the corresponding architecture component. For this 
aim, add the following to your module’s build.gradle file:
implementation "android.arch.lifecycle:livedata:1.1.0"
This LiveData object now allows for adding an observer as follows:
val ld: LiveData> =
employeeDao.getAllSync()
ld.observeForever { l ->
l?.forEach { empl ->
Log.e("LOG", empl.toString())
// do s.th. else with the employee
}
}
This is particularly useful if inside the observer callback you update GUI components.
A LiveData object also allows for adding an observer tied to a lifecycle object. This is done 
with the following:
val ld: LiveData> =
employeeDao.getAllSync()
val lcOwn : LifecycleOwner = ...
ld.observe(lcOwn, { l ->
l?.forEach { empl ->
Log.e("LOG", empl.toString())
// do s.th. else with the employee
}
} )
For details about lifecycle objects, please take a look at the online API documentation for 
android.arch.lifecycle.LiveData.
Caution Your production code should do a better job in doing correct housekeeping. The 
LiveData object should have the observer unregistered by calling ld.removeObserver(...) 
at an appropriate place in your code. It is not shown here because we just provide snippets, and the 
housekeeping must be done in the code 
containing the snippets.


125
CHAPTER 8: APIs
A similar but maybe more comprehensive approach to add observables to your database 
code is to use RxJava/RxKotlin, which is the Java/Kotlin platform implementation of 
ReactiveX. We do not give an introduction to ReactiveX programming here, but including it in 
queries boils down to wrapping the results into RxJava objects. To give you a quick idea of 
how to do that, you, for example, write the following:
@Query("SELECT * FROM employee" +
" WHERE uid = :uid")
fun findByIdRx(uid: Int): Flowable {
[...] // Wrap query results into a Flowable
}
This returns a Flowable, allowing observers to react on retrieved database rows in an 
asynchronous manner.
For this to work, you have to include RxJava support into the build file (remove the newline 
after implementation).
// RxJava support for Room
Implementation
"android.arch.persistence.room:rxjava2:1.0.0"
For more details about RxKotlin, please consult the online resources about ReactiveX in 
general or RxKotlin for the Kotlin language binding of ReactiveX.
Database Clients
To actually include Room into the app, we need to know how we can get hold of databases 
and DAO objects. To achieve this, we first acquire a reference to a database via the 
following:
fun fetchDb() =
Room.databaseBuilder(
this, MyDatabase::class.java,
"MyDatabase.db")
.build()
val db = fetchDb()
This creates a file-backed database. The string argument is the name of the file holding the 
data. To instead open a memory-based database, say for testing purposes or because you 
favor speed over data loss when the application stops, use the following:
fun fetchDb() =
Room.inMemoryDatabaseBuilder(
this, MyDatabase::class.java)
.build()
val db = fetchDb()



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   106   107   108   109   110   111   112   113   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling