I am developing an android app. Each activity binds to a service that is used to do all the actual work. Normally, everything works great - but not when an array with about 100 000 objects arrives from the server as an update.
After about 10 000 objects are inserted the DB thread and even the UI thread hangs. The processor load is never more than 60%, memory used is less than 200 MB.
I do all my DB work in a separate thread on the service; I make sure not to overload the Db queue, extracting objects from the JsonArray just enough so that there are always about 100 objects in DB thread queue.
When the big array with updates arrives from the server as a Json Array, I gradually disassemble it and work with each object separately.
Db queue uses Room to access the database.
`@Update(onConflict = OnConflictStrategy.FAIL)`
`public abstract void update(User localTransport);`
There are no transactions.
Ids of all DB objects are stored in a HashMap so I may choose to update or insert a row with no overhead.
Here is my update function:
private long doUpdateOrInsert(Sendable s) {
if (Core.core.dbHelper.storage.hasItem(s)) {
dbDao.updateSendable(s);
return s.id;
}
else {
long id = this.dbDao.insert(s);
Core.core.dbHelper.storage.addItem(s,id);
return id;
}
}
Please, could you tell me how to solve this problem? Why does my UI thread hang? I don't get anything unusual in logs except for ANR cause UI is unresponsive for five seconds and my logs that my DB thread stops processing messages.
Edit:
Sorry for not stating clearly, all my work is done on a Service. It is separate from activities, launched with startService()
, and is bound to the current activity. Db Thread is run by a separate thread in the Service.
Here are the screenshots of profiler after app randomly died and returned to the previous screen even though I din't touch the phone, without even writing anything to the app logs.