Friday 26 February 2016

Working with blocks

Block is same as the c function.The basic idea of a block is to treat a small piece of code as if it were a value. The piece of code can then be passed as a parameter in messages or assigned to a variable.

^{NSLog(@"It is a block literal); }


The use of blocks often involves nothing more than including code in block literal form as parameters for methods (designed to take blocks) that are part of the iOS API.
A block is the only object that begins its life in stack, rather than heap memory.A very effective use of a block literal is to deal with a callback. 

Creating a block variable:

int  (^sum)(int, int) = ^(int a , int b){
 return a+b;
}

This  is a simple block definition . It takes two parameters  of type int and return one int parameter.
To call this block as:

int c = sum(3,5);
NSLog(@"sum is %d",c);

When a block is created, it will capture, or close around, the values of those variables that the code has referred to and that are in the same lexical scope of the block. Essentially, a snapshot is taken of these values. These values are preserved in the memory allocated for the block and cannot later be changed. It is said that these variables are "captured by value".
To change the this behavior(to use the latest values of the variable), we can declare these variable as 
__block storage specifier.

__block int z =5;

Block as method parameter: We can also use block as method parameter to use call back. For example:
 
-(void)validateString:(NSString *)str onCompletion:(void(^)(bool isValidate))completion{
          
 if(str.length ==0){
    completion(NO); //call block
}else{
    completion(YES);
}
}

Now call this method as 

[self validateString:@"iOS" onCompletion:^(bool isValidate){
    
if(isValidate){
 //str is valid, handle it
}else{
  // str is not valid, handle it here
}       
   
}];

That's it. Though the use of block seems some complex at first time but once we get used to, it is very easy to use block in our code. With the help of block we can make our code simple, readable and easy to maintain .

Friday 19 February 2016

iOS app life cycle

We can categorized iOS life cycle mainly in two category:

1) The App launch cycle
2) View Controller life cycle

The launch cycle states different app states in which an iOS app can transit in its life . The different app states are as:

1) Not running
2) Inactive
3) Active
4) Background
5) Suspended

When an app goes from one states to another it is called app delegate methods. These methods provides a way  to take appropriate action  on app state changes. These methods are as:

application:didFinishLaunchingWithOption: called when app launches first time.

applicationDidBecomeActive: called after app enters in foreground.

applicationWillResignActive: called before an app enters in background.

applicationDidEnterBackground: called after app enters in background. In this state an app may be suspended at any time.

applicationWillEnterForeground:called when app changes state from background to foreground. But app is not yet active

applicationWillTerminated: This method is not called when an app is suspended or user terminated the app explicitly.


View Controller life cycle:

viewDidLoad: called only once  during initial load of the interface.

viewWillAppear:This method is called just  before views appears or renders on screen every time when you   navigate or switch between different views.

viewDidAppear: called when views completely.  Here we can handle UI or functionalities.

viewWillDisappear: called before view is removed or any animation is configured.

viewDidDisappear:called when view is removed.

Friday 12 February 2016

Property Attributes in Objective C

Property attributes gives the functionality to define a way in which the property works. With attributes we can change the way property works. These property attributes can be categorized into three category.

1) Atomocity
       atomic and nonatomic
2)Access
       readonly and readwrite
3) Storage
      strong, weak , assign and copy


Atomic(default):
       An atomic property guaranteed that you will get a valid data when you try to read from it. It does not make any guarantees what the data might be but you will get the good data not just junk memory. This is used when multiple thread or processes pointing to same variable and reading and writing simultaneously.

Nonatomic:With it you lose the guarantee that you will get good data every time . When you try to access in the middle of the write, you could get back the garbage data.


ReadOnly: It makes the property read only that means no setter for it at all.

ReadWrite(default): It is the flip side of the readonly. Allows both read and write.

Strong(default): It means you have a reference to an object and you will keep the object alive. As you hold the reference to the object , the object will not  be deallocated and released back to the memory.

Weak: It gives the reference to the object so that  you can talk to the object but you can not keep it alive. If object's reference count goes to zero, the property becomes nil automatically.

Assign:It is used for primitives.

Copy:It works well with all kinds of mutable objects If you set a copy property, instead of just setting both references to the same object, what it actually does is makes a copy of the object you are setting and then sets the property to that copy.