Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FirestorePagingAdapter setSnapshotArray #1750

Closed
VincentJoshuaET opened this issue Mar 19, 2020 · 7 comments
Closed

FirestorePagingAdapter setSnapshotArray #1750

VincentJoshuaET opened this issue Mar 19, 2020 · 7 comments

Comments

@VincentJoshuaET
Copy link

Currently, I am using FirestoreRecyclerAdapter on my RecyclerViews to save their scroll positions upon fragment destroy (I use Android Jetpack Navigation Components).

This is how I do it: https://proandroiddev.com/fui-rv-state-a66833c3bb88

I want to switch to FirestorePagingAdapter to improve the performance of my app, but it seems that I cannot save my recyclerview's scroll positions because FirestorePagingAdapter always queries for the data upon start.

@VincentJoshuaET
Copy link
Author

VincentJoshuaET commented Mar 30, 2020

Update: I did manage to use modify FirestorePagingAdapter and FirestorePagingOptions to use the existing LiveData from my ViewModel:

ViewModel:

private val config =
    PagedList.Config.Builder()
        .setEnablePlaceholders(false)
        .setPrefetchDistance(2)
        .setPageSize(2)
        .build()
private val factoryPetShop = FirestoreDataSource.Factory(firestoreRepo.getPets(), Source.DEFAULT)
val liveDataPetShop = LivePagedListBuilder(factoryPetShop, config).build()

FirestorePagingOptions:

fun build(data : LiveData<PagedList<DocumentSnapshot>>, modelClass: Class<T>): FirestorePagingOptions<T> {
    val parser = ClassSnapshotParser(modelClass)
    return FirestorePagingOptions(data, ClassSnapshotParser(modelClass), DefaultSnapshotDiffCallback(parser), mOwner)
}

I know this can be better, but this is just what I need for my app. But still, hoping this feature can be added.

@im-not-alex
Copy link

Update: I did manage to use modify FirestorePagingAdapter and FirestorePagingOptions to use the existing LiveData from my ViewModel:

ViewModel:

private val config =
    PagedList.Config.Builder()
        .setEnablePlaceholders(false)
        .setPrefetchDistance(2)
        .setPageSize(2)
        .build()
private val factoryPetShop = FirestoreDataSource.Factory(firestoreRepo.getPets(), Source.DEFAULT)
val liveDataPetShop = LivePagedListBuilder(factoryPetShop, config).build()

FirestorePagingOptions:

fun build(data : LiveData<PagedList<DocumentSnapshot>>, modelClass: Class<T>): FirestorePagingOptions<T> {
    val parser = ClassSnapshotParser(modelClass)
    return FirestorePagingOptions(data, ClassSnapshotParser(modelClass), DefaultSnapshotDiffCallback(parser), mOwner)
}

I know this can be better, but this is just what I need for my app. But still, hoping this feature can be added.

How did you extend FirestorePagingOptions if it is final? did you fetch and modified it? If yes how do you rebuild it to be a library into a project?
Thanks

@VincentJoshuaET
Copy link
Author

Update: I did manage to use modify FirestorePagingAdapter and FirestorePagingOptions to use the existing LiveData from my ViewModel:

ViewModel:

private val config =
    PagedList.Config.Builder()
        .setEnablePlaceholders(false)
        .setPrefetchDistance(2)
        .setPageSize(2)
        .build()
private val factoryPetShop = FirestoreDataSource.Factory(firestoreRepo.getPets(), Source.DEFAULT)
val liveDataPetShop = LivePagedListBuilder(factoryPetShop, config).build()

FirestorePagingOptions:

fun build(data : LiveData<PagedList<DocumentSnapshot>>, modelClass: Class<T>): FirestorePagingOptions<T> {
    val parser = ClassSnapshotParser(modelClass)
    return FirestorePagingOptions(data, ClassSnapshotParser(modelClass), DefaultSnapshotDiffCallback(parser), mOwner)
}

I know this can be better, but this is just what I need for my app. But still, hoping this feature can be added.

How did you extend FirestorePagingOptions if it is final? did you fetch and modified it? If yes how do you rebuild it to be a library into a project?
Thanks

Yep. Just copied the whole files into my project. I didn't rebuilt the libraries.

@mandelmech
Copy link

Does this also solve the fact that the adapter re-fetches the data every time you leave and come back? I'm having tremendous trouble fixing this. Whenever I click an item and come back to the list, the query is re-run, same with orientation changes.

@VincentJoshuaET
Copy link
Author

Does this also solve the fact that the adapter re-fetches the data every time you leave and come back? I'm having tremendous trouble fixing this. Whenever I click an item and come back to the list, the query is re-run, same with orientation changes.

It does it for me. The data set is saved and scroll position is restored.

@mandelmech
Copy link

I see you use a FirestoreDataSource and pagedlist, can you share how you query to make the datasource work? Thanks!

@samtstern
Copy link
Contributor

This has been released in 7.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants