0

I am new to swift and trying to learn when I saw apple doc in it there is a definition about escaping closure like:

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns. When you declare a function that takes a closure as one of its parameters, you can write @escaping before the parameter’s type to indicate that the closure is allowed to escape.

@ other hand I saw a blog where blogger explained about completion handler like:

There are times you put a closure as one of the parameters, but you only want to execute the closure after the function returns. For example, what if you want to print “Hello, world” only after you’ve completely switched to the next ViewController?

So I want to know if completion handler and escaping closure are same or what?

D4ttatraya
  • 3,344
  • 1
  • 28
  • 50
V-Dev
  • 488
  • 1
  • 4
  • 16

2 Answers2

0

I want to know that completion handler and escaping closure are same or what?

Completion and Error Handlers are related to a process that should be done after executing -and returning- a chunk of code (function):

Completion handlers are callbacks that allow a client to perform some action when a framework method or function completes its task. https://developer.apple.com/library/content/featuredarticles/Short_Practical_Guide_Blocks/

which are -logically- representation of escaping closures.

So, referring to the escaping closures definition, a completion handler is by default is an escaping closures.

For reviewing example(s), you might want to check this answer. Also, if you think that you need more description about what is the escaping closure, you might wan to check this Q&A.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • Thanks. This is little bit easy to understand. but m not clear completely about this topic. I think i need to spend more time on it. But your answer helped me. :) – V-Dev Jul 05 '17 at 09:30
  • Glad to help :) you could shoot a comment if any needed help related to understanding escaping closures; I hope that the answers I mentioned to be useful... – Ahmad F Jul 05 '17 at 09:33
0

Completion handlers are one example where escaping closures are passed, but they are not the only one.

In fact, the concept of a completion handler works on a different level than the concept of an escaping closure.

Escaping Closure

The closure is stored, and may be invoked at some time in the future.

This is in contrast to a non-escaping closure, where the closure gets called immediately or not at all.

The difference between escaping and non-escaping closure is important for memory management. For non-escaping closures, the compiler knows that any resources used by the closure aren't used anymore after the call.

Completion Handler

A completion handler is a closure that gets called once an operation is finished.

Operations that use completion handlers often are long-running and work on different threads. While such an operation is running, your program can continue with its control flow, thereby making the program more responsive. For example, if you call URLSession.downloadTask(withResumeData:completionHandler:), you will schedule a new download task in URLSession. Downloading may take a while, depending on your connectivity status and other factors, and only once the download was finished your completion handler gets called.

The way such operations are implemented is usually, that they store the completion handler somewhere, perform the operation, and then call the completion handler. Therefore, completion handlers are usually escaping closures.

nd.
  • 8,699
  • 2
  • 32
  • 42