Sunday, 26 April 2015

BackGround Threading.

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.

         

Tuesday, 21 April 2015

IOS-AppStates

App States:[5]

1. Not  Running-When the app is not launched or terminated by the user;

2.Inactive State:When the app is running in the foreground but not receiving any events.

3.Active State:When the app is running in the foreground and also receiving events.

4.BackGround States:When the app is running in the background and is also executing codes.

5.Suspended State:When the app is running in the background and not executing code. 

Frames and Bounds

"Ok,what is the difference between bounds and frame?"asks every interviewer.

Here's the answer:

Bounds:
             Bounds of a UIView is the rectangle,expressed as a location(x,y) and size (width,height) relative to it's own co-ordinate system.

Frame:
          Frame of a UIView is the rectangle,expressed as a location(x,y)and size(width,height)relative to the superview,it's contained within.

To make it more clearer,let's create a label and will check the output:



Now in NsLog,let's check for the corresponding values of frames and bounds:




So the output for the above is: