tags:

views:

45

answers:

3

I have a Network Client class that is receiving a large binary block and parsing it into a usable Java object. The Network Client is on a separate thread from the app's View. What is the best way to make this object available to the View? I've come up with the following solutions, but I feel like none of them are the correct one:

  1. Create the object in the Network Client and let the View access it directly
    • I would send a small message in a Handler telling the View that the data has been updated
    • Con: requires that I synchronize the object between the threads to ensure that the Network Client doesn't replace the object while the View is accessing it
  2. Serialize (Parcel?) the object in the Network Client and send it through a Handler to the View
    • Pro: there are no questions of ownership of the data
    • Con: would probably be a huge performance drain on the app
  3. Create a reference to the object and pass that to the View
    • I come from a C++ background, and I'm not sure if this is even possible in Java. I C++, I could just send the View a pointer to the object and let it take care of it. That seems like something Java wouldn't let me do. Is this feasible?

Are any of these solutions advisable, or should I approach the problem in a completely different way?

A: 

If you don't want to keep downloading when the activity is in the background, then use non-blocking IO, not threads.

If you do want to keep downloading when the activity is in the background, you probably want to use a service. You can make the object Parcelable or so; I think the underlying service implementation passes pointers around if your activity and service are within the same process (I think they are by default, but ICBW).

tc.
That's a very good question that I hadn't given much thought. I guess my Network Client needs to be a Service; if I am not downloading in the background, the socket I am listening on will be completely full of old data when the app resumes. Since I'm going to turn the Network Client into a Service it needs to Parcel-ize any data that it wishes to share with the Activity, correct? There's no alternative ways?
Topher
If you are using a local service you can just create getter and setters and communicate with the service like any other object. Search for local service for more information.
Janusz
As I said, it depends. If they're running in the same process, the underlying implementation will (I think) try to pass pointers around instead of copying data.
tc.
I was able to pass the objects around with direct getters and setters. Thanks guys!
Topher
A: 

If the object is really big and you don't feel comfortable returning it with a get method, maybe you could put its contents into an SQLite database and optionally expose it as a ContentProvider. You could also send an Intent and either cause the View to then go and grab the payload or attach it to the Intent.

Frank
Good answer, but I'd like to avoid using external storage like SQLite.
Topher
You don't have to use external storage with SQLite, you can use your /data/data/com.domain.appname/ directory. See this link for more information: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Frank
A: 

Look at the application class subclassing this class and referencing this within your manifest will enable you to store the reference to the service/download controller at a central position that will be available in every activity of your app. This enables you to keep the data in memory and reduce the need of recreating the big object if you need it in more places then just one activity.

For the download you can use a local service that communicates with your activity through a binder object. Keep in mind that a service is not a thread. If you want have the download running in the background you need to create a thread in the oncreate method of your service.

Also keep in mind that it is good practice to have an annotation show the user that a service is doing something and let him access the service and cancel it or view it status.

Janusz