Listen to Realm Background data changes in Clean Architecture (Realm + Flow + Clean Architecture)

Tram Ho

Preamble

I like to use realm in my project because of its speed and flexibility when it comes to listening to changes in data from the database, but that’s not when I’m working on a clean architecture based project because of its independence. between floors in the project.

Way

Is there any way? Yes, and there are many, let’s try listing it out:

Realm + Broadcast Receiver —> is too resource-intensive, need to write a listen subscribe function and unsubscribe from Broadcast Receiver

Realm + Live Data —> is good, but the live data is of android and the business layer (Domain) should not contain android in it.

Realm + Rx —> good, but requires the entire thread to be processed in the main thread (affecting the UI)

So is there any other way, I was desperate until Flow in Coroutines appeared the flexibility in multithreading that attracted me to a great discovery when I found a solution for it =)), now Let’s try to code

Code

Code Base

Code in Database

Code in Repository

Code in UseCase

Code in ViewModel

Code in Activity

References

Think of it myself =))), maybe I will write an article on medium

Conclusion

I hope to help you in working with realm in the clean architecture. If you have any questions, don’t hesitate to comment on the article, I will try to answer. Thanks

Share the news now

Source : Viblo