Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between Completion Handler and Blocks : [iOS]

I am messed with both completion handler and blocks while I am using them in Swift and Objective-C. And when I am searching blocks in Swift on google it is showing result for completion handler! Can somebody tell me what is the difference between completion handler and blocks with respect to Swift and Objective-C ?

like image 941
shubham mishra Avatar asked Sep 21 '16 07:09

shubham mishra


People also ask

What is completion handler in iOS?

A completion handler in Swift is a function that calls back when a task completes. This is why it is also called a callback function. A callback function is passed as an argument into another function. When this function completes running a task, it executes the callback function.

What is difference between completion handler and closure in Swift?

As an example, many functions that start an asynchronous operation take a closure argument as a completion handler. The function returns after it starts the operation, but the closure isn't called until the operation is completed—the closure needs to escape, to be called later.

What is a completion block?

The block to execute after the operation's main task is completed.

What are completion blocks in Swift?

Swift Closures with Completion handler Closures are self-contained blocks of functionality that can be passed around and used in your code. Said differently, a closure is a block of code that you can assign to a variable.


1 Answers

Here you can easily differentiate between blocks and completion handlers in fact both are blocks see detail below.

Blocks:

Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary.

  • They can be executed in a later time, and not when the code of the scope they have been implemented is being executed.
  • Their usage leads eventually to a much cleaner and tidier code writing, as they can be used instead of delegate methods, written just in one place and not spread to many files.

Syntax: ReturnType (^blockName)(Parameters) see example:

int anInteger = 42;  void (^testBlock)(void) = ^{      NSLog(@"Integer is: %i", anInteger);   // anInteger outside variables  };  // calling blocks like testBlock(); 

Block with argument:

double (^multiplyTwoValues)(double, double) =                            ^(double firstValue, double secondValue) {                                return firstValue * secondValue;                            }; // calling with parameter double result = multiplyTwoValues(2,4);  NSLog(@"The result is %f", result); 

Completion handler:

Whereas completion handler is a way (technique) for implementing callback functionality using blocks.

A completion handler is nothing more than a simple block declaration passed as a parameter to a method that needs to make a callback at a later time.

Note: completion handler should always be the last parameter in a method. A method can have as many arguments as you want, but always have the completion handler as the last argument in the parameters list.

Example:

- (void)beginTaskWithName:(NSString *)name completion:(void(^)(void))callback;  // calling [self beginTaskWithName:@"MyTask" completion:^{      NSLog(@"Task completed ..");  }]; 

More example with UIKit classes methods.

[self presentViewController:viewController animated:YES completion:^{         NSLog(@"xyz View Controller presented ..");          // Other code related to view controller presentation...     }]; 

[UIView animateWithDuration:0.5                      animations:^{                          // Animation-related code here...                          [self.view setAlpha:0.5];                      }                      completion:^(BOOL finished) {                          // Any completion handler related code here...                           NSLog(@"Animation over..");                      }]; 
like image 137
vaibhav Avatar answered Oct 11 '22 11:10

vaibhav