Skip to main content

Posts

Showing posts from 2015

Blocks are Objective-C Objects !!!

Blocks Blocks are executable code just like a function.  It can be written inside a function or we can store the reference in a variable and call it later.  But wait a minute these all functionality we can achieve by function pointer and functions.  Then what is so special about blocks? Let first go through how we can define blocks. int (^sum)( int , int ) = ^( int number1, int number2){ return number1+number2; }; The above block can be used to find the sum of two numbers.  If we recall the function pointer in c then it is very much similar to above: int sumFunction ( int number1, int number2){ return number1+number2; } int (*sum) ( int , int ) = sumFunction; //Function Pointer *sum Then Why we need Blocks if we already have Function pointer?  1) B locks can capture variables from the enclosing scope by simply referring to them within the block. How ? Let see the next Section 2)  Blocks are Objective-c Objects not

dispatch_async vs dispatch_sync

GCD: A library to provide support for concurrent code execution on multicore hardware in iOS. It provides an easier mechanism to access multi-thread environment without taking the overhead of lock and unlock shared data, create a thread and perform a task like that. If we use  NSThread or pthread in the application then we need to take care of so much point. Like if I need to execute 10 tasks concurrently in my application. How much thread pool I require, how to synchronise two tasks while using threads. It provides methods to add a task in a queue. 1) Dispatch Async 2) Dispatch Sync  Dispatch Async: This method is used to add a block for asynchronous execution and return immediately. If you used this method to add a block then it will return immediately and does not wait for execution of the block. If you do not want to wait for the execution of the block then you can use this method to add operation in a dispatch queue. - ( void ) performMyTask{        dispatc

UIViewController view is overlapped with the Navigation and Status bar

Background: Since iOS7 release status bar is part of your  the view. It is the common problem faced in application that navigation and status bar overlapped with your view content when you use nib or xib files.                 XIB                                          Actual result I have seen in some application that to fix this problem view content is start from 64 pixel down. But it can raise other problem in the applications. So question is: How To Fix This ? Solution 1: In View did load you can add the following line: [ self setEdgesForExtendedLayout : UIRectEdgeNone ]; Example: - ( void )viewDidLoad {     [ super viewDidLoad ];     // Do any additional setup after loading the view, typically from a nib.     [ self setEdgesForExtendedLayout : UIRectEdgeNone ]; } edgesForExtendedLayout This property tells which sides of your view can be extended to cover the whole screen. If you push a UIViewController  into a UINavigationContro

How to Handle Multiple Asynchronous Response (Wait to complete All)

Background : Suppose you are developing an application and you need to hit multiple Asynchronous request at a time. Now you are waiting for the responses from all the request to be complete. Question is When you received any response in a delegate, callback or Completion block, how to ensure whether all the request is completed or not ? There can be many ways to do this. I am explaining here two ways to handle this: 1) Use a simple Counter (Preferred for same type of request) 2) Use a enum with Bitwise Method Logical Explanation: Use a simple Counter: In this approach we need to use a counter. For every request we will increase the counter by 1 and when we receive the response, decrement it by 1. This approach is good if we have only one type of request. Like image downloading for gallery. In this approach we cannot differentiate that which request is completed or which is not. Use a enum with Bitwise Method In this approach we will use enum to keep t

How Cross Platform works in native world

Cross Platform: Cross platform means here single application or codebase run on multiple mobile platform(iOS, android). Today most of the organization focusing on mobile world. When an organisation take first step in the domain the very first question in there mind is. Should i go with native development or cross platform? There Decision based on the some factors Native (Pros):  1) Native Look and feel 2) High Performance 3) No restriction over device capability (Like camera, Bluetooth) etc and Many more Native (Cons): 1) More resources require to develop native application. i.e more money 2) Training require for each platform. CrossPlatform (Pros): 1) Developer need to know only single technology or language (Javascript and Html).  2) Less resources require for developing and maintaining application. 3) Distribution can be done like other native apps (App store, Play store)      CrossPlatform (Cons): 1) Application developed

Deep Diving Objective-C Properties (Part 2)

Previous post of properties explained about the lifetime qualifiers. Today post will have exciting stuff about the other qualifier and some concepts about where to used these qualifier. Threads and Properties: Suppose you are working in multithreaded environment and two thread want to access the same property at same time. Thread A want to write and thread B is in middle of reading the property name.  What happen which value thread B will get. ? There are two qualifier which decide the behaviour in such condition. 1)  atomic (Default) 2) nonatomic Atomic: 1) Default behaviour. 2) Ensure that one thread will access property or method at a time. (Inconsistency will be not there as atomicity). Thread B will get autorelease value always in the above case. 3) Does not ensure the thread safety of instance variable. Any other thread can access the instance variable bind to property. So instance variable in not thread safe only setter and getter is thread safe.