https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
v

Viktor Petrovski

09/16/2021, 3:37 PM
Hello 👋 I’m working on some sample project testing things and I wondered how would you guys approach the following pretty common scenario: We have a Feed of posts and a post detail screen that can be open from the feed. We can also like the post, when we like the post from post detail when we go back we also want to update the feed for the post we’ve liked. If we are not using Realm or any DB what would your approach be?
a

Alex Prince

09/16/2021, 5:47 PM
my approach would be a room DB w/ livedata bindings to both but, I think the key is that both of them are reading from / updating the same source of truth, then you don't have to worry about it. Without livedata you're pretty much down to either communicate back through the activity, or use an event bus of some kind to communicate changes ( this will tend to bite you after a while, been there, done that )
👍 1
v

Viktor Petrovski

09/18/2021, 5:43 AM
Thanks for the suggestion, I was able to solve it in a different way. I’ve made a cache repository and everytime a post changes I publish changes to that repository and every viewmodel who is interested listen for changes.
Copy code
class PostsCacheRepositoryImpl : PostsCacheRepository {
    private val _postFlow: MutableSharedFlow<Post> = MutableSharedFlow(
        replay = 1,
        onBufferOverflow = BufferOverflow.DROP_OLDEST
    )
    override val postFlow: SharedFlow<Post>
        get() = _postFlow

    override fun publishPostUpdate(post: Post) {
        _postFlow.tryEmit(post)
    }
}

interface PostsCacheRepository {
    val postFlow: SharedFlow<Post>
    fun publishPostUpdate(post: Post)
}
2 Views