Objective:
To understand background threading and perform a simple Math with and without background threading.
Why we need to use background threading?
When we build an application,we need to be extremely aware of the processing that goes in the UIThread,else if the process is too time intensive,then the user interface may not respond or may become unresponsive and it will lead to poor user experience.
Understanding the need for background threading:
To understand the problem just create this either programmatically or use Xib or StoryBoard,The desirable UI is:
Two buttons:-
1.Process in ForeGround;
2.Update Label;
One Label:-
1.Label;
Approach:
Now if we run the program,and when we tap the Process in ForeGround button,and immediately if we tap update label button to check the time,the update label will not start performing operation until the process in foreground operation button stops.This leads to poor user performance because the user interface remains unresponsive until particular operation is completed.The reason this happens is because we're doing all the processing within the method directly called by the UI,it's preventing anything from being processed.So,even though you've tapped update label button,it won't respond.
So to overcome this we use backGround threading.
Create another button Process in BackGround.
Add the following code in the button method:
We first call dispatch_async and send in the dispatch_get_global_queue method to get the DISPATCH_QUEUE_PRIORITY_BACKGROUND.The background queue is kind of a default very low priority queue that can be used for low priority processing.dispatch_get_global_queue is used to get shared concurrent queues,of which background queue is one of them.The background queue will be scheduled only after executing the high priority queues(if present)
After the loop we have created another method dispatch_async .In this call we call dispatch_get_main_queue and pass in a block which will call BackgroundOperationsDone method.The dispatch_get_global_queue gets the application's main queue which the UI is running on.And once the operations are completed,to get back to the main UI thread we are using the background operations method.
Conclusion:
Now run the code,we will be able to tap both the buttons[process in BackGround & UpdateLabel] simultaneously and both will perform it's task without any time lag.and the user experience will also be good.This is just the tip of the iceberg,lot can be done using threading.
To understand background threading and perform a simple Math with and without background threading.
Why we need to use background threading?
When we build an application,we need to be extremely aware of the processing that goes in the UIThread,else if the process is too time intensive,then the user interface may not respond or may become unresponsive and it will lead to poor user experience.
Understanding the need for background threading:
To understand the problem just create this either programmatically or use Xib or StoryBoard,The desirable UI is:
Two buttons:-
1.Process in ForeGround;
2.Update Label;
One Label:-
1.Label;
Approach:
Now if we run the program,and when we tap the Process in ForeGround button,and immediately if we tap update label button to check the time,the update label will not start performing operation until the process in foreground operation button stops.This leads to poor user performance because the user interface remains unresponsive until particular operation is completed.The reason this happens is because we're doing all the processing within the method directly called by the UI,it's preventing anything from being processed.So,even though you've tapped update label button,it won't respond.
So to overcome this we use backGround threading.
Create another button Process in BackGround.
Add the following code in the button method:
We first call dispatch_async and send in the dispatch_get_global_queue method to get the DISPATCH_QUEUE_PRIORITY_BACKGROUND.The background queue is kind of a default very low priority queue that can be used for low priority processing.dispatch_get_global_queue is used to get shared concurrent queues,of which background queue is one of them.The background queue will be scheduled only after executing the high priority queues(if present)
After the loop we have created another method dispatch_async .In this call we call dispatch_get_main_queue and pass in a block which will call BackgroundOperationsDone method.The dispatch_get_global_queue gets the application's main queue which the UI is running on.And once the operations are completed,to get back to the main UI thread we are using the background operations method.
Conclusion:
Now run the code,we will be able to tap both the buttons[process in BackGround & UpdateLabel] simultaneously and both will perform it's task without any time lag.and the user experience will also be good.This is just the tip of the iceberg,lot can be done using threading.