Whats the proper way to fill a recyclerView with data from a proto datastore

27 views Asked by At

I cannot fill my recyclerView with data fetched from my proto datastore.

I have a proto datastore setup and working, meaning I can read and write data to it.

My recyclerView is set up following the android developer guide: https://developer.android.com/develop/ui/views/layout/recyclerview#implement-adapter The custom adapter is consuming a list of typed Object generated with a proto.schema file and the protobuf gradle plugin.

Iam working in fragments and using the default android lifecycles (onCreateView, onViewCreated, etc.).

My approach was to "simply" fetch the data from my datastore (which works), transform said data to a list and provide this list to my adapter, which inflates my recyclerView and displays the created entries.

First I thought this would be a quick task since the kotlin language guide looks very straight forward:

myFlow
    .onEach { entryList.add(it) }
    .onCompletion { println("Done") }
    .collect()

https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/on-completion.html

Since collect() is a suspend function and I can only call it from a coroutine or another suspend function I wrapped this in a lifecycleScope: ps. I did not make 'onViewCreate' suspend because I would have had override complications.

val entryList = mutableListOf<TypedEntry>()
    lifecycleScope.launch {
        context.dataStore.data
            .onEach { entryList.add(it) }
            .onCompletion { recyclerView.adapter = CustomAdapter(entryList.toList()) }
            .collect()
    }

With this setup I am able to retrieve and then add every entry to my list but 'onComplete' never seems to execute and my adapter is never supplied with data.

I am aware that there are asynchronous action and that this might be the main pain point. I also tried the async - await approach which unfortunately also did not work.

Because I am not aware if this is the right setup I am wondering if this is even the right approach (datastore -> list -> adapter).

I am refraining to make a code dump here but if you have any further questions regarding my setup do not hesitate to ask!

Cheers

p.s. first post here. I hope it adheres the guidelines.

0

There are 0 answers