Swift Closures: @escaping Explained
Summary
TLDRThis video delves into Swift's escaping and non-escaping closures, focusing on their practical differences and usage scenarios. Sponsored by DevMountain, a boot camp for coding and design, the presenter uses a network call example to illustrate the necessity of an escaping closure when the function's execution extends beyond its scope, such as waiting for a network response. The video also touches on the importance of avoiding retain cycles when using escaping closures, providing a clear explanation of when and why to use each type of closure in Swift.
Takeaways
- 😀 The video discusses the concept of escaping and non-escaping closures in Swift and their practical applications.
- 🏫 The video is sponsored by DevMountain, a coding and design boot camp offering various programs including iOS development.
- 🎓 The presenter is a boot camp graduate and has been an iOS developer for five years, providing credibility to the discussion.
- 🔗 DevMountain offers career services and financing options, and they are interested in feedback from the presenter's viewers.
- 📞 The 'escaping' keyword in Swift is used in the context of closures that need to outlive the function they are passed to, such as in network calls.
- 🌐 The example given is a network call to a GitHub API to fetch a list of followers, which is a common scenario for using escaping closures.
- 🕊️ Non-escaping closures are executed immediately within the scope of the function they are defined in, without needing to 'escape'.
- ⏱️ Escaping closures are used when a task, like a network call, takes time to complete and the closure needs to wait for the result.
- 🔄 The presenter mentions automatic reference counting in Swift, which is crucial for understanding how escaping closures work in memory.
- 🔄 The video touches on the importance of avoiding retain cycles when using escaping closures, as they maintain a reference to the function's context.
- 👨🏫 The presenter offers to create courses and invites viewers to check out their site for more educational content.
Q & A
What is the main topic of the video?
-The main topic of the video is about explaining the concept of escaping and non-escaping closures in Swift.
What does the term 'escaping' refer to in the context of closures in Swift?
-In the context of closures in Swift, 'escaping' refers to a closure that needs to outlive the function it is passed to, often because it is used asynchronously, such as in a network call.
Why is the 'escaping' keyword used in the 'get followers' function example?
-The 'escaping' keyword is used in the 'get followers' function example because the closure needs to execute after the function has completed, specifically after a network call has returned its data.
What is the difference between escaping and non-escaping closures?
-Escaping closures are those that need to outlive the function they are passed to, often due to asynchronous execution. Non-escaping closures, on the other hand, are executed immediately within the scope of the function they are passed to.
What is the common scenario presented in the video where an escaping closure is used?
-The common scenario presented in the video where an escaping closure is used is making a network call, such as fetching a list of followers from an API.
Why is it important to understand the difference between escaping and non-escaping closures?
-Understanding the difference between escaping and non-escaping closures is important to manage memory correctly and avoid retain cycles, especially when closures are used in asynchronous tasks.
What is the role of the 'self' reference in the context of escaping closures?
-In the context of escaping closures, the 'self' reference is used to prevent retain cycles and to ensure that the closure has access to the instance of the class or struct it was created in, as it may outlive the original function's execution.
What is the sponsor of the video mentioned in the script?
-The sponsor of the video is DevMountain, an in-person coding and design boot camp that offers housing at no extra cost for full-time immersive students.
What additional services does DevMountain offer besides their iOS development program?
-DevMountain offers additional services such as programs in web development, software QA, and UX design, as well as a career services team to help with job placement and financing options.
What is the significance of the 'weak self' capture list in the context of the script?
-The 'weak self' capture list is used to prevent strong reference cycles in closures that capture 'self'. It ensures that the closure does not inadvertently keep a strong reference to the instance it is part of, thus avoiding memory leaks.
How does the video help the viewer understand the practical application of escaping closures?
-The video helps the viewer understand the practical application of escaping closures by demonstrating a real-world example of making a network call to fetch data and then using the closure to update the UI once the data is received.
Outlines
📚 Introduction to Escaping and Non-Escaping Closures in Swift
The video begins with an introduction to the topic of escaping and non-escaping closures in Swift programming language. The presenter explains the difference between the two and sets the stage for a detailed explanation. The video is sponsored by Dev Mountain, a boot camp offering various programs including iOS development, web development, software QA, and UX design. The presenter, a boot camp graduate, highlights the career services and financing options available at Dev Mountain and encourages viewers to check out the provided link for more information.
🔗 Understanding Escaping Closures with a Network Call Example
The presenter dives into the concept of escaping closures using a network call as an example. They illustrate a function named 'getFollowers' that makes an API call to retrieve a list of followers and display them in a collection view. The closure in question is marked as 'escaping' because it needs to outlive the function's execution time, waiting for the network response. The explanation clarifies that an escaping closure is used when the closure's execution extends beyond the scope of the function it's passed to, such as in the case of asynchronous operations like network calls.
🔄 The Importance of Reference Cycles in Escaping Closures
The video continues with a deeper look at the implications of using escaping closures, particularly the issue of retain cycles. The presenter explains that because the closure is executed after the function has completed, it must maintain a reference to the view controller to update the UI and perform other tasks. This leads to a discussion about 'weak self' and 'unowned self' capture lists to prevent retain cycles, which can cause memory leaks. The presenter also mentions a previous video on automatic reference counting for those unfamiliar with the concept.
Mindmap
Keywords
💡Escaping Closures
💡Non-Escaping Closures
💡Swift
💡Network Call
💡API
💡CollectionView
💡Automatic Reference Counting (ARC)
💡Retain Cycle
💡DevMountain
💡Career Services
💡Financing Options
Highlights
Today's video discusses escaping and non-escaping closures in Swift.
The video is sponsored by DevMountain, a coding and design boot camp offering housing and career services.
The presenter is a boot camp graduate and has been an iOS developer for five years.
DevMountain offers programs in web development, software QA, and UX design, in addition to iOS development.
The presenter uses a network call example to explain the use of 'escaping' closures in Swift.
A closure is marked as 'escaping' when it needs to outlive the function it is passed to.
The 'escaping' keyword is explained in the context of an API call to retrieve a list of followers.
Closures can either execute immediately within the function's scope (non-escaping) or after the function has completed (escaping).
The presenter emphasizes the importance of understanding the scope and lifetime of closures in Swift.
The video provides a practical demonstration of a network call and how it relates to escaping closures.
The concept of 'escaping the lifetime of the function' is discussed in detail.
The presenter explains the use of 'weak self' to prevent retain cycles when using escaping closures.
The video clarifies the difference between escaping and non-escaping closures with a real-world coding example.
The importance of managing memory and references when working with closures is highlighted.
The presenter provides a summary of when to use 'escaping' and when to use 'non-escaping' closures.
The video concludes with a call to action for viewers interested in learning more about iOS development.
The presenter invites viewers to check out their own courses for further learning.
Transcripts
today we're talking about escaping
closures in Swift we're gonna talk about
the difference between non escaping and
escaping what that actually means and
when to use it but first today's video
was sponsored by Devon mountain dev
mountain is an in-person coding and
design boot camp that offers housing at
no extra cost for full-time immersive
students now if you know my story I
myself am a boot camp grad it was part
of the process to help launch my iOS
developer career which I'm going on year
five now in this wonderful profession
but aside from their iOS development
program dev Mountain also offers
programs in web development software QA
and UX design they even have a career
services team to help you with job
placement and financing options are
available in dev Mountain loves hearing
from my viewers so if you or someone you
know is ready to start this journey into
iOS development be sure to check out the
link in the description all right back
to the video so I'm gonna use a very
common circumstance on when you would
use at escaping to explain this and that
is a network call here you can see this
function get followers real quick let me
show you what's actually going on on the
screen because I'd like to give the
context here we are hitting the get up
API we're getting a list of followers
and we're showing it in this collection
view and I don't know I always like to
show that stuff because just looking at
code I like to see the context of what
the code is actually doing helps me
understand what's going on so there you
go that's what's going on so about this
escaping keyword here that you see so as
you can see we have function get
followers we pass in a username page
whatever but here's our closure right
and it returns a result that has an
array of followers or an error right
this is how we populate our collection
view well we tagged this closure at
escaping now why did we do that well
when you pass in a closure as a
parameter and a function which is what
we're doing right we have a parameter
called completed and here's our closure
it has it can do one of two things it
can be executed like immediately within
the scope of this function which you
know to refresh you the scope is this
curly brace down to this curly brace
which is basically everything that's
happening and get followers so one
option is to have that closure execute
immediately within the scope of that
function that would be non escaping
right because it doesn't have to escape
so think about the word escape I just
said it five straight times
it's basically escaping the life
time of this function so what that means
is is this closure needs to you'll often
hear it called use at escaping if the
closure has to outlive the life of the
function so when this function get
followers gets called all this code
executes however what this code is
executing on is this is making a network
call this is why this is a common
example so when you make a network call
right we requested the github API hey
give me the list of followers and then
we download that list right that takes
time if you're on good Wi-Fi may take
half a second if you're on bad cellular
service that could take seven seconds
right so our closure which is what we
call to execute you know once we get our
list back that's how we populate our
collection view our closure needs to
outlive the life of this function
because it's sitting around like waiting
for the network call to come back and
while it's out there waiting for the
network called to come back right it's
got to live somewhere and it so it lives
in memory so because closures are
reference types so what that means is
there some automatic reference counting
going on and because it's out there
living in memory that reference count
has been incremented to one side note if
you're not familiar with automatic
reference counting I did a video all
about that I'll put the link in the
description so if we go to where get
followers here is called on the follower
list VC you'll see Network managers not
shared get followers you'll see we have
the we called it result that's what our
closures you know coming back as and you
can see we're using the week self
capture list that is to you know prevent
some retain cycles but back to the
closure kind of living by itself in
memory write it it outlived the life of
its original function that is why it
needs to have a reference to self to do
all this you know dismiss loading view
update UI etc so you know it has its own
reference to this view Co self being
this view controller earth to follower
list VC so that's something you need to
keep in mind and be careful of the
retain cycle aspect of things when using
these escaping closures because again
they're out there living in memory by
themselves so they have to have
references to you know whatever they're
using so back to the network manager
again the summary when you use add
escaping is when the closure needs to
outlive the life of the function that
it's a part of and like I just showed
you a super common example of that is
doing networking so hopefully this video
helped clear up escaping vs. non
escaping remember non escaping is it'll
execute like a meet
within the scope hopefully I cleared all
that up if you enjoyed my teaching style
my presentation I check out the site on
the screen I started creating my own
courses we'll talk to you the next video
Weitere verwandte Videos ansehen
![](https://i.ytimg.com/vi/KLW5ip4mcsM/hq720.jpg)
More Closure Examples | JavaScript 🔥 | Lecture 129
![](https://i.ytimg.com/vi/3a0I8ICR1Vg/hq720.jpg)
Learn Closures In 7 Minutes
![](https://i.ytimg.com/vi/qnDxLcFMxJ4/hq720.jpg)
Closures | JavaScript 🔥 | Lecture 128
![](https://i.ytimg.com/vi/m1gDEmXPAAM/hq720.jpg)
Algorithm Design | Network Flow | Ford-Fulkerson Algorithm | MAXIMAL FLOW PROBLEM | MAX FLOW PROBLEM
![](https://i.ytimg.com/vi/1srJz5IjkGs/hq720.jpg)
NW LAB 1. Basics of Socket Programming in C : TCP and UDP - Program Demo
![](https://i.ytimg.com/vi/AaxenoqTXxc/hq720.jpg)
Pooling and Padding in Convolutional Neural Networks and Deep Learning
5.0 / 5 (0 votes)