Saturday, 21 February 2015

HB Blog 59: Asynchronous Operations In Android And iOS.

Asynchronous Operations In Android:-
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute. AsyncTask must be subclassed to be used. The subclass will override at least one method (doInBackground(Params...)), and most often will override a second one (onPostExecute(Result).)

The three types used by an asynchronous task are the following:
1)Params, the type of the parameters sent to the task upon execution.
2)Progress, the type of the progress units published during the background computation.
3)Result, the type of the result of the background computation.

When an asynchronous task is executed, the task goes through 4 steps:
    onPreExecute() - It invoked on the UI thread before the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface. We can stop further background process in case of webservice parsing while no internet connection is found.
    doInBackground(Params...) - It is invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.
    onProgressUpdate(Progress...) - It is invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing.
    onPostExecute(Result) - It is invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

Here is skeleton for AsyncTask,
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Async_DemoTask extends AsyncTask<String, String, String> {
  
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    // Doing something on the main thread before the task is executed
       //start progress bar
       //check internet connection
    }

    @Override
    public String doInBackground(String... params) {
       // Perform long running process
        return response;
    }

    @Override
    protected void onPostExecute(String success) {
        super.onPostExecute(success);
       //stop progress bar
       //Update the UI
    }
}

Asynchronous Operations In iOS:-
Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in iOS and OS X. With GCD, you can line up blocks of code in a queue for the system to execute as necessary. These blocks or operations, will be dispatched in the queue to another thread, leaving your main UI thread to continue its tasks.

dispatch_queue_create - It creates a new dispatch queue to which blocks can be submitted.
dispatch_get_main_queue - It returns the serial dispatch queue associated with the application’s main thread.This queue is created automatically on behalf of the main thread before main is called. This method is useful for updating UI.

Here is skeleton for dispatch_async,
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Doing something on the main thread
dispatch_queue_t demoQueue = dispatch_queue_create("demo queue",NULL);
dispatch_async(demoQueue, ^{
    // Perform long running process
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update the UI       
    });
});
// Continue doing other stuff on the
// main thread while process is running.

No comments:

Post a Comment