sockeqwe / sqlbrite-dao

DAO for SQLBrite
http://hannesdorfmann.com/android/sqlbrite-dao
Apache License 2.0
182 stars 22 forks source link

a large number of list data insert into database , how to update UI? #32

Closed sharyuke closed 8 years ago

sharyuke commented 8 years ago

when I get a large number of list date and insert they into database. I just can use protected Observable<Long> insert(final String table, final ContentValues contentValues) ; this method . but it just can insert one item . As soon as I insert one item , the UI callback which update UI .will be called again.so If I insert a large number of list data, the callback will be called many times in a short time.and rxjava will throw rx.exceptions.MissingBackpressureException .

How can I fix it?

sockeqwe commented 8 years ago

Hi, do you know what Backpressure is? If not, basically your problem is that inserting elements are done on one thread, and updating the UI is done on another thread (main UI thread). The problem is, that inserting elements into database is faster then updating the UI. Hence there is a holdup. RxJava detects this holdup and crashes. This problem can be solved by applying a "holdup" (aka. backpressure) resolution. There are some opperators build in in RxJava like sample() or throttleLast() (http://reactivex.io/documentation/operators/sample.html)

However, there might be a better solution. Instead of inserting all items one by one you can do "bulk inserts". This can be done by a transaction (sqlbrite-dao provides transactions) or you can use sqlbrite to execute your own SQL bulk INSERT (better performance) by using sqlbrite.executeAndTrigger() method, see here . As trigger parameter you have to use your table name (where you are inserting the new elements)

sharyuke commented 8 years ago

I think i got it ..thanks a lot again!