Skip to main content


Showing posts from January, 2020

What I learned from Kotlin Flow API

I used to check the docs and just read a lot about flows but didn't implement anything until yesterday. However, the API tasted really cool (even though some operations are still in Experimental state).Prerequisites: If you don't know RxJava it's fine. But a RxJava recognizer would read this faster.Cold vs Hot streamsWell, I really struggled with this concept because it is a little bit tricky. The main difference between cold and hot happened to be pretty simple: Hot streams produce when you don't care while in cold streams, if you don't collect() (or RxJava-s equivalent subscribe()) the stream won't be activated at all. So, Flows are what we call cold streams. Removing the subscriber will not produce data at all, making the Flows one of the most sophisticated asynchronous stream API ever (in the JVM world). I tried to make a illustration of hot and cold streams: Since I mentioned the word asynchronous this implies that they do support coroutines also. Flows vs…

Searchable Fragments with the Paging Library

This post is inspired by @EpicPandaForce answer in StackOverflow. I faced the same problem which I didn't know how to solve: How to perform search when you are using a Paging Library (or how the hell to refresh after I reperform Rooms query)?Let's suppose we have this scenario: I have a list of data, which are shown in the Fragment by LiveData observation, which are retrieved by the ViewModel through LiveDataPagedListBuilder(). I'm hoping you know the basics of the Paging Library already.The data source:I'm retrieving the data using a local database and Paging Library's Datasource.Factory<Key, Value>:@Dao interface MyDao{ @RawQuery(observedEntities = [MyEntityRepresentation::class]) fun selectAllMeetingCondition(query: SupportSQLiteQuery): DataSource.Factory<Int , MyEntityRepresentation>: }Room queries at Runtime.My use case requires to generate a dynamic query each time the user performs a search. In other words, the query depends on the users …

Prepopulate Room with data.

There are times, when we just need the data when the app starts, and all the functionality is just a matter of work. Or we just need the app to be independent from the network and we have the data. A simple dog-race database or cat-race database doesn't actually need online interaction at all (if there are not too many data of course). So, Room comes with a nice solution about this. The docs are pretty clear and short when it comes to this topic.
We just write:

Room.databaseBuilder(appContext, AppDatabase.class, "Sample.db") .createFromAsset("database/myapp.db") .build() And our data are ready to be instantiated when the app starts. One more thing to note is that its extremely fast to do it this way. However, here are some problems that the docs don't even bother to mention (which I think are important for some).
1 - A .db file, is not a .sql file. The file we are supposed to hold in assets folder which contains a query like: CREATE TABLE IF NOT …