I am trying to use LiveData for paging data from my Room database. The sample code from Google indicates the use of a toLiveData function:
class ConcertViewModel(concertDao: ConcertDao) : ViewModel() {
val concertList: LiveData<PagedList<Concert>> =
concertDao.concertsByDate().toLiveData(pageSize = 50)
}
https://developer.android.com/topic/libraries/architecture/paging
Here is the code I created that follows their example:
AppDao.kt
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy.REPLACE
import androidx.room.Query
import com.example.model.UserConnection
@Dao
interface AppDao {
@Query("SELECT * FROM UserConnection")
fun getAll(): LiveData<List<UserConnection>>
@Insert(onConflict = REPLACE)
fun save(userConnection: UserConnection)
@Query("SELECT * FROM UserConnection ORDER BY firstName DESC")
fun connectionsByFirstName(): DataSource.Factory<Int, UserConnection>
}
AppDatabase.kt
import androidx.room.Database
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.model.UserConnection
@Database(entities = arrayOf(UserConnection::class), version = 1, exportSchema = true)
@TypeConverters(RoomConverters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun appDao(): AppDao
}
ConnectionsViewModel.kt
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.paging.DataSource
import androidx.paging.PagedList
import com.linkedintools.da.local.room.AppDao
import com.example.model.UserConnection
import javax.inject.Inject
class ConnectionsViewModel @Inject constructor(appDao: AppDao) : ViewModel() {
val connectionsDataSource : DataSource.Factory<Int, UserConnection> = appDao.connectionsByFirstName()
val concertList: LiveData<PagedList<UserConnection>> = connectionsDataSource.toLiveData(pageSize = 40)
}
build.gradle
dependencies {
def room_version = "2.1.0-rc01"
def paging_version = "2.1.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// Paging
implementation "androidx.paging:paging-runtime:$paging_version"
}
The toLiveData is not available. Am I doing something wrong or is the sample code from Google out of date?
PagingSource object defines a source of data and how to retrieve data from that source. A PagingSource object can load data from any single source, including network sources and local databases.
The Paging Library lets you load data directly from your backend using keys that the network provides. Your data can be uncountably large. Using the Paging Library, you can load data into pages until there isn't any data remaining. You can observe your data more easily.
toLiveData is an extention fun, import this library and it is all done :
androidx.paging:paging-runtime-ktx:2.1.0-rc01
Addionaly check this google samples project Paging With Network Sample
https://github.com/googlesamples/android-architecture-components/tree/master/PagingWithNetworkSample
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With