toLiveData not available from DataSource.Factory in Android's Paging
Asked Answered
P

1

12

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?

Persecution answered 31/5, 2019 at 7:19 Comment(1)
got to read those comments carefully here --> developer.android.com/topic/libraries/architecture/paging#setupClutter
T
28

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

Tropical answered 31/5, 2019 at 9:34 Comment(1)
Thank you! I don't know about *-ktx libraries of androidx until reading your answerPhosphoprotein

© 2022 - 2024 — McMap. All rights reserved.