Andrew
Andrew

Reputation: 21076

IntentService getting wedged

I've built an application which uses an IntentService for manipulating the application's database and calling web services.

Example:

I have an Activity which is a list of items. This list is populated by a CursorAdapter. Each item has a checkbox and the list is ordered by checked (unchecked items on top and checked items on bottom). When an item's checkbox is checked, I send an Intent to my IntentService telling it to flag the checked column in the database table for that item, then a message is Broadcasted, the Activity sees it and re-queries the Cursor.

When I'm using a mobile device and start rapidly checking checkboxes, the app essentially hangs. The UI doesn't hang, but the IntentService in the background does. I can tell because the Cursor on screen isn't getting refreshed.

It is my understanding that Intents passed to IntentServices are not processed asynchronously. Is that correct? It's behaving almost like I need to be using semaphores, but I can't understand why if Intents are queued and processed individually.

Suppose I check 3 checkboxes; so there are 3 Intents queued. After the first one I broadcast a message, which the Activity receives, telling it to requery it's cursor. Could a potential problem be because I'm requerying the Cursor at the same time I'm processing the second Intent (updating another row in the table)? Querying the Cursor happens on the UI thread.

^ I think this is my problem. What should I use to synchronize this? Semaphores? Can someone point me to some documentation/examples?

Another potential issue is what if my Activity manages a Cursor on a table; also my IntentService queries a Cursor from the same table. I have instances where this happens as my IntentService is going through each row in the Cursor to find items to send to the web service. Though I wouldn't think this would be a problem so long as the Cursors don't query at the same time?

Does anyone have any other ideas?

Upvotes: 1

Views: 1225

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007604

When an item's checkbox is checked, I send an Intent to my IntentService telling it to flag the checked column in the database table for that item, then a message is Broadcasted, the Activity sees it and re-queries the Cursor.

This has a "using a Buick to swat a fly" feel to it.

It is my understanding that Intents passed to IntentServices are not processed asynchronously. Is that correct?

onHandleIntent() in your IntentService is called on a background thread, and startService() is always asynchronous.

It's behaving almost like I need to be using semaphores, but I can't understand why if Intents are queued and processed individually.

Individual Intents are queued and processed serially by the IntentService.

Could a potential problem be because I'm requerying the Cursor at the same time I'm processing the second Intent (updating another row in the table)?

This certainly does not help.

What should I use to synchronize this? Semaphores?

I'd just use synchronized(someStaticDataMember), but semaphores should work.

Though I wouldn't think this would be a problem so long as the Cursors don't query at the same time?

The actual query is not executed until the Cursor is touched (e.g., moveToFirst(), getCount()). After that, so long as the query result set is under 1MB, the results are entirely in the Cursor.

I agree in part with Christopher: use some logging to figure out exactly what is going on, and consider making this a bit more lightweight (e.g., AsyncTask for the database write rather than an IntentService).

Upvotes: 1

Related Questions