Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

confused between syncAdapter,services,loaders,providers and asynctask? [closed]

I am new to android and was going through the documentation and some tutorials.

Services

Developer guide says it should be used, when there is required to run a long running task in background, say music player.

Asynctask

Creates a worker thread to perform background task like getting data using api and then notify the UI thread on it's onPostExecute() callback method.

Loaders

Performs operations on separate thread, registers to the listener and notifies on data set changes.

Providers

Share data between different apps by exposing them in the manifest file.

SyncAdapter

For synchronizing data between android device and web server.

Theoretical wise, i understand the above concepts like what they are used for. I am having difficulty to put them in order like when to use what? what would be the best among them to use? In what scenarios what should be used?

For caching, i used sqlite or library like volley and retrospice

As i said i am android beginner and try to understand these concepts.

Any helps and suggestions would be appreciated. Thankx in advance.

like image 474
Ritt Avatar asked Oct 12 '15 17:10

Ritt


2 Answers

A couple of things to add to Kaleb's answer:

ContentProvider:

Android has actually many ways to share data. The ContentProvider is a way to share a database between applications. Let's say you have 3 email clients on your phone that cache data from the cloud in case you're offline. It makes sense that you maintain only one offline database of your emails. The same goes if you have an address book, an sms database, or a tweet database. And the same goes if you want to update all that data. You only really want to update that data only once from the cloud, not three times each time, which brings me to the next topic, the SyncAdapter.

SyncAdapter:

A SyncManager is smart enough to do all the synchronization requests in one go, to minimize the time the antenna is powered, and therefore to save battery use. Furthermore, with the help of the AccountManager, the user of the phone can override the synchronization preferences himself/herself, but that's only if the developer chose to use a SyncAdapter in the first place.

Also, a SyncAdapter can only be used with a ContentProvider. So even if you don't want to share your data with other apps, you'll have to use a content provider if you want to use a SyncAdapter.

That being said, ContentProviders are very hard for beginners (even intermediate developers) to implement. I'd suggest you stay away from them for now. You should use a ContentProvider, if there is already one for what you want to do, but I don't recommend you try to create your own.

Loaders:

Loaders are good. Learn to use them if you want to display data from a local database (or from a ContentProvider). They'll save you time. Unlike the SyncAdapter, loaders do not require a ContentProvider for them to work. They can access SQLite directly.

Services:

Learn to use them. There are too many things to say about them. One important point is that you should minimize the time they stay alive by using components like AlarmManager or BroadcastReceivers. Also, you'll need to learn the difference between a Service and an IntentService.

AsyncTask:

AsyncTask is very often necessary in Android to avoid blocking the main UI thread. Don't think that because you're using an AsyncTask, that you can dispense with the use of Services.

Note that many Android tutorials only give you the minimum amount of code to demonstrate a concept, so they'll often skip proper threading. Note that you can start your own thread manually if you want, but AsyncTask does other things for you that make it the ideal choice for many situations where the UI thread gets blocked and you get an "Application Non Responding" error.

Libraries:

There are many good libraries out there. I won't say which ones are the good ones. Learn to use the ones that everyone recommends. Those libraries can do a lot for you (assuming you're good enough to make them work). There is a bit of a learning curve, but it's worth it. And they deal with Android at a much higher level of abstraction, so usually, the threading and many other things are usually taken care for you.

There are many other things that I am glossing over, or that I didn't mention at all, but like I said, I think your question is too broad. And if you really want more details, you should hunker down and read some the developer guides and watch some of the youtube videos provided by Google.

like image 120
Stephan Branczyk Avatar answered Nov 17 '22 20:11

Stephan Branczyk


Really quick answer :

AsyncTask : short task that could block the UI thread. You don't mind if they are cancel and you have to re launch them.

Services : use when you have long task that you don't want to be interupted by App changes. Bit harded to implement than AsyncTask.

Loaders : designed for database access

SyncAdapter : here you don't have realtime data. You schedule data sync at a give moment (i.e sync mails, contact data, etc...). Let's say you have fresh data every hour.

Providers : nothing to do with the above. This is used to share data beetween Apps. You don't care how data is retrieve by the sharing App, you just know that you can ask for a given resource.

This infographis helped me understand better the first 3 : https://raw.githubusercontent.com/stephanenicolas/robospice/master/gfx/RoboSpice-InfoGraphics.png

like image 20
Kalem Avatar answered Nov 17 '22 18:11

Kalem