Reputation: 9929
I have a Service
which runs in the background when my app starts, and continues to run after the app is closed. It implements
a Loader.OnLoadCompleteListener<Cursor>
for a registered Uri
from a Content Provider
. When a new Cursor
object is delivered on the callback method onLoadComplete(Loader<Cursor> loader, Cursor data)
I then run an AsyncTask
using the Cursor
to create an ArrayList
of custom Objects
that implement the Parcelable
interface
. Once processed, if the app is open it sends back to the App getting marshalled/demarshalled using the IPC framework using a Handler
with Messenger
,Messages
and aBundle
. If the App is not open then the ArrayList
is ready to be sent back to the App once opened, which calls to the Service
when opened.
Problem:
The ArrayList
can get relatively large (it doesn't contain Bitmaps
, only Primitives
and several short String
objects) and the sheer amount means it hits the FAILED BINDER TRANSACTION
when the ArrayList
gets to about 700 objects.
Current Solution (that feels a bit hacky)
I am splitting the ArrayList
into chunks and sending several Messages
back to the Handler
where it is then reconstructed into one ArrayList
and then used in the App (updating RecyclerViews etc..).
I use this approach as the performance is significantly improved - only need to do initial query when App/Service is started first time, rather than always querying the Content Provider
every time the App is opened.
The reason I say 'hacky' is because this feels like a workaround for a limitation of the Binder Framework
rather than a solution.
Any advise on approaching this differently would be welcomed.
Upvotes: 1
Views: 3833
Reputation: 1995
Two alternatives that come in my mind is:
Having a static ArrayList inside your service and sending a broadcast when the cursor received to the Activity to copy the contents of the static Arraylist which is inside the service in the local arraylist inside the activity. With this way Activity only has reference of the static aarraylist only the time it copies the contents.
I would save the contents in sql database asynchronously and then send broadcast to activity to retrieve asyncrhonously again the cursor from the database. On the UI thread then I would call notifydatasetChanged on the adapter.
Upvotes: 1