two years past, there comes fragment, intent service, cursor loader. Is the approach still up to date, or is there any better or mature pattern to design an android rest client, especially compare to the option B (I don't have the privilege to post image, instead the image could be found from this post).
I know the content provider part is essential. what about the service helper and service component? Up till now, the startService method is a nature of Context or its subclasses. which means the service helper would be an activity. So is it elegant to initiate an activity from a content provider, or should it be initiate from the activity on the top.
There comes service, intent service, asyncTask and thread. In my opinion, the intent service is suitable for sync of mega pack of data from remote server. That why they use it in the iosched. But the common scenario is that only a part of items will be synchronized with the remote server. So the intent service is too heavy. even the service approach. could we just use the asyncTask or thread in the content provider or some component of that to accomplish this kind of task. Or is there any convincing reason to use the service, and go through the service helper-service-processor path. I am talking about a serious application.
so what's you opinion?
So is it elegant to initiate an activity from a content provider, or should it be initiate from the activity on the top.
You'll never ever initiate an activity from the content provider. Everything should start from your activity, whether that's a AsyncTask, Service, or Content Provider request...
AsyncTasks are generally a poor choice. They're simply flawed when it comes to dealing with configuration changes (i.e. screen orientation change). Loaders are the solution to this, but the difficult part is wrapping that together with your network calls. One solution is to build the network calls from a custom loader (subclassing AsyncTaskLoader).
However, in my case, I followed the 2010 Google IO presentation. Built a class, ServiceHelper, to manage requests to the server in a Service object (which launches threads to perform network queries). The ServiceHelper manages the ResultReceivers which can be created from the calling Activity. This allows the activity to listen to events from the Service request such as when the query starts and finishes (or when it errors out). These threads will call their network queries and then store the resulting data in a ContentProvider (for caching and to use across multiple Activities if necessary).
At the same time, I have a CursorLoader on the Activity that listens to the endpoint which the network thread will write to. Obviously there's a lot of middle ground to work out yourself.. such as your caching policy and the overhead for such an implementation. But that really depends on the app you're building and the API you're integrating with.
So yes, I think that the 2010 presentation is still valid.. there were a lot of vague areas of his presentation and the continues today. You'll have to still work out a design that works for your app
Hopefully, my thoughts will help you get started..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With