devadnqpnd
devadnqpnd

Reputation: 156

To use AsyncTask or Service?

This is the scenario, in my app I have a story post feature.While creating a post,a user can attach a photo.Then, what I want is to upload the photo to my server after a user selects a photo from the gallery, not after the user clicks the post button. After the upload was done a callback will give information of whether the upload was successful or not. Also, if there are photos being uploaded to the server then a user click the post button, all that photos that are being uploaded will be cancelled.

Pseudo Code

List tempList = [photoUri,photoUri,photoUri] 
List attachedPhotoUriList.copyValue(tempList);

for each photoUri in tempList

    uploadPhotoToServer(attachedPhotoUri,callback{
           onSuccess:
              attachedPhotoUriList.remove(photoUri)
           onError:
    })

QUESTION: Where can I do the uploading of photos in order to detach it from the main thread then returns a callback if a photo was been successfully uploaded or not in an activity? and is cancelable?

In AsyncTask or Service? Though the answer is not limited to these two,you can suggest anything that you think is appropriate.

Upvotes: 0

Views: 66

Answers (2)

AlbAtNf
AlbAtNf

Reputation: 3909

If you use AsyncTask, you have to take care of orientation changes. To me this is not really the nicest way.

I think it is much cleaner to use a Service. More specific an IntentService.

This Service is designed to handle one time tasks on its own thread (normal Service runs on UI thread). You start it by sending an Intent with the Uri of your image as an extra. Inside the IntentService you handle the received Intent with the extras (Upload the image from the Uri). When done, you notify your Activity by sending a broadcast to that Activity (of course you have to register a listener for that). A very good tutorial is here. The official guide from Google is here.

Once started, you don't have to take care of the orientation changes.

Canceling an IntentService is a bit more tricky, but can be done. But maybe you should just let it upload and discard it on server side, if no fitting post was uploaded after a while (or some other mechanism).

Services with AIDL are not needed in your case, because the Activity and the Service are running in the same process.

Upvotes: 0

voghDev
voghDev

Reputation: 5801

Have in mind that a Service runs on UI thread by default, so if you use it, you should create and manage your own new Thread, which implies more lines of code to maintain. An IntentService runs on its own thread, so I would prefer this one rather than the Service, and the AsyncTask.

What I'd suggest, is to use a third-party library like retrofit which manages the connection details for you, if that's possible considering your project's requirements; In this tutorial you can see how to upload files to a server, retrofit takes care of the UI thread issues, doing the request outside it, and returning the response in it, so you can update your views.

Upvotes: 1

Related Questions