https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
v

Vaibhav Jaiswal

02/23/2024, 7:33 AM
Is there any state management library for KMP with flow and PagingData support?
j

Joel Denke

02/23/2024, 8:52 AM
A little bit uncertain what refer to as state management library, but if refer to only paging and Flows there is androidx paging compose 🙂 Just check to use version https://developer.android.com/jetpack/androidx/releases/paging#3.3.0-alpha02 or later to get proper KMP support 🙂
v

Vaibhav Jaiswal

02/23/2024, 8:54 AM
@Joel Denke the thing is As of now i am using SQLDelight for managing state, so for example if i like a post in one screen I can see it liked in all other screens, thanks to state management But this DB is getting quite heavy and overkill for my use case, i dont want to persist data, just need in-memory state management
j

Joel Denke

02/23/2024, 8:57 AM
Not entirely sure what you want to have, but sounds to me want to introduce cache/repositories then 🙂 Maybe https://square.github.io/workflow/ is suitable for your case.
v

Vaibhav Jaiswal

02/23/2024, 9:01 AM
I'll check this out Basically what i want is Redux toolkit but for KMP with Flows and PagingData support
j

Jilles van Gurp

02/23/2024, 10:31 AM
fritz2 has nice state management based on flows.
j

Joel Denke

02/23/2024, 10:39 AM
Maybe I am totally wrong here, but feels like you want to checkout UDF if this is a compose multiplatform ui? See https://developer.android.com/jetpack/compose/architecture#udf Sorry if I am mistaken, maybe not using Compose. But if using that, I think its what you looking for.
v

Vaibhav Jaiswal

02/23/2024, 1:18 PM
Yes @Joel Denke i am using compose and following udf too, android docs suggest using a db which is fine, but what if I dont need a persistent storage but just an in memory one Room allows it if im not wrong not sure about SQLDelight
j

Jeff Lockhart

02/23/2024, 2:48 PM
You can use SQLDelight with a database driver configured for in-memory. But if you're not actually doing any data querying or persistence, I agree you probably don't need such a library. Can you not just use
MutableStateFlow
directly? Paging in-memory data isn't really necessary, since it's already present in memory. The purpose of paging is to only load a subset of total data into memory at once.
By the way, my JSON database KMP library, Kotbase, also supports flows and paging, but it's also primarily for data persistence.
v

Vaibhav Jaiswal

02/23/2024, 3:00 PM
I do query data My setup uses Paging3, I have a Remote Mediator and ui is using
LazyPaginItems
How would "not using `PagingData`" work? By "using
MutableStateFlow
" do you mean keeping a mutable state flow in the singleton repository class and emitting in it after an API call and all ViewModels collecting from that?
Thanks, I'll also take a look at Kotbase
j

Jeff Lockhart

02/23/2024, 3:06 PM
You could use a
MutableStateFlow
in a repository for a reactive in-memory data store, yes. What sort of data is it that you don't need to persist? And what type of queries are you doing? Is this data coming from a backend and you're just caching it locally in memory?
v

Vaibhav Jaiswal

02/23/2024, 3:16 PM
Example of query
Copy code
SELECT * FROM PostEntity
WHERE creatorId = :creatorId
ORDER BY createdAt DESC
LIMIT :limit OFFSET :offset;
My main use-case of having a db is to observe changes in data in all screens, persistence is not needed, I clear db on app start Data comes from a backend, and I save it in SQL Delight DB to observe changes everywhere in app Example, if I like a post, i make api call then update it in DB, so everywhere its shown as liked
6 Views