Completion Handlers
Вставка
- Опубліковано 22 сер 2024
- In this video I am going to explore CompletionHandlers. What are they? How can we create them, and how to use them. They are a fundamental concept in programming and are often referred to as callbacks.
As we proceed, we will also learn about escaping completion blocks, and trailing closures.
We will start with a playground as an introduction to develop the concept and end up with looking at a simple iPhone app to reinforce our learning.
Starter Projects:
Part 1 Playground
www.createchso...
Part 1 iPhone Project
www.createchso...
Reference Video for Static Class functions
• Instance and Static Me...
Stewart Lynch's UA-cam Channel
/ stewart Lynch
CreaTECH Solutions Website
www.createchso...
Stewart Lynch's GitHub
github.com/Ste...
The clearest explanations on UA-cam! Thanks for sharing this.
I rarely comment on youtube videos, but I truly cannot thank you enough for this explanation. Thank you very, very much
You are welcome Jenny. I learn through teaching so these videos are made as much for my benefit as they are for anyone else. I refer often to the videos I have made over the past year. I try to craft my explanations so that they can be easily understood.
Read and watched everything on completion handlers and closures INCLUDING Apple's documentation but still was incredibly confused -- UNTIL this video (and your one on closures). Thank you!!!!
Steward man, you're the best Swift Tutor I have come across on all of Internet. I hope you have made videos on concurrency.
I have actually. But I did them for the CodeWithChris channel. You can find them at ua-cam.com/play/PLMRqhzcHGw1a4jFHEBitPwCtAgPxWldfy.html
Such an intuitive explanation of Closures.
Your explanation is simply the best - thank you very much!
Wow. This was amazing. The explanation, the progress from simple examples to more complex ones to real life scenarios. Also love the rectangles bouncing around the code to help show the various code flows. Amazing. If all your content is like this, I’m watching it all!!!!
Glad you liked it! Yes, they are all like this LOL. At least I try.
so clearly, so easy to understand, so friendly to beginner. very helpful, really thanks.
Glad you enjoyed it.
God I wish I watched this video back when I started! Absolutely perfect! the best explanation ever. Thank you, now I don't have to explain this to anyone I'll just share this video with them
Thanks again Mohamed. I really appreciate the positive feedback. It keeps me motivated.
Very useful tutorial, Completion handlers never looked so easy.
Best explanation i have ever seen.Thankyou please make more n more content.👏👏
Thanks, If you have any suggestions for topics, let me know. I want to focus on single topic ideas that give new developers some problems
I bet it must have taken you a lot of time to create this video. It's all so well explained. You have become my #1 Swift youtuber. Thanks!
Thanks. Yes, I spend a lot of time preparing for and recording my videos. Often a 30 min video takes 3 days to complete.
The most precise and well - explained tutorial about C.H I have ever found.
Thanks for clean explanation and simple code sample. Very useful
Now that's how you make a tutorial video! Awesome job Stewart!
Thanks Colby
MUCH MUCH CLEAR THAN ANY OTHER TUTORIAL.... NOW I KNOW WHATS COMPLETION HANDLER
Wow. This is the first time I’ve understood. Thank you.
Glad you found the video Jim and found it helpful.
May GOD bless on you for this effort.
I really appreciate this tutorial! Thank you so much!
Amazing explanations of closures and completion handlers. very much appreciated Stewart.
Thanks for the feedback.
Perfectly taught tutorial after a long time! Thank you for this man.
Thank you very much for this video. You are helping a lot.
Thank you for the Best description of completions handlers! It helps a lot!
Glad it was helpful!
Wow, You explained it so well.
Simply the best explanation, thank you very much! Subscribed.
Thanks. I try :)
Thanks for your step-by-step explanation from the very very basic to the basic but practical use case. Great help for the beginners.
Wow, what a lifesaver! I'm having the exact problem reloading a tableview with data from my PHP server. I've watched about 15 videos on closures and completion handlers, but none of them really clicked in my head as to what was going on. Your methodical approach to teaching really helped make it click. I'll be using your code as a template for adding completion handlers to several places in my app. Thank-you! Liked and subbed!
Best description of completions handlers I have seen so hats off to you Stewart.
Thanks Chris. Much appreciated.
@@StewartLynch
I have a request that you might consider in a future video if you think that it's interesting enough to be a topic.
The scenario I had in mind is as follows:
The initial ViewController is a TableViewController and is embedded in a NavigationController.
A second ViewController is added to serve as a means of adding a new record or edit an existing record. It would have a Cancel button and a Save button in the navigation bar. A fairly typical scenario I would have thought.
In the case of "Edit", lets say you pass a few parameters to the AddEditViewController:
mode - which is an enum - (.edit or .add),
the array which is the data source of the tableView
and the other is the index which you can derive from didSelectRowAt.
So you would call the AddEditViewController by:
if let editVC = storyboard.instantiateViewController(identifiedBy: "AddEditView") as? AddEditViewController {
editVC.mode = .edit
editVC.dataArray = dataArrray
editVC.rowIndex = indexPath.row
navigationController.pushViewController(editVC, animated: true)
}
In the case of "Add", which is triggered by a navigation button "+", which might have the code:
@objc addNewRecord() {
if let addVC = storyboard.instantiateViewController(identifiedBy: "AddEditView") as? AddEditViewController {
addVC.mode = .add
addVC.dataArray = dataArrray
navigationController.pushViewController(addVC, animated: true)
}
}
In .edit mode you would Save the data to the array passed in and in .add mode you Save the new item to the array (append). In either case, how would you pass back the array which had been modified? Using a completion handler?
chrispy_wa In this case, I would inject the enum and a closure into the pushed VC and in that vc handle the add/edit by using Service Class function that would handle all data CRUD operations. The closure would just be a func that would either update or reload the tableview depending on the case. Funny you should mention this idea as I was thinking that this would be a good follow up to this tutorial. I have a couple different ones planned but I will add this to the list.
@@StewartLynch
Cheers Stewart.
Very useful tutorial, love to see tutorials related to Concurrency. Thank you Steward.
On my list
love your explanations!!
Thank you so much! Great video
Thank you very much. It was really useful.
Thank you very much Stewart, a truly great teaching style.👌🏽
great video thanks Stewart!
Good Explanation, thanks
Its was really helpful. Thanks
Best explanation I have found ! Holyy smokess!
Take a look at my other videos too. I try to be clear.
Hi, Stewart! I’m really enjoying these videos. Even though I’m already an iOS developer who works with Swift on a daily basis I still learn some new things from them. But mostly, you are a very good presenter/educator. Keep up the great work! On a separate note: I don’t know why, but this video’s audio seems to be playing only through the left channel on my earphones. I don’t know if this is intentional, or just a side effect of how you recorded your voice or encoded the video, but it’s a bit distracting to me so I wanted to let you know.
Thanks for your comments, especially since you are an experienced developer. Much appreciated. I just plugged in my earbuds to listen to this video and there does seem to be a bit more coming through on the left channel, but it is not exclusively on the left channel, but definitely more. Not sure how that happened as I recorded on both channels. I will try to monitor that in subsequent videos before I post. Thanks again.
great video Stewart
Excellent! Liked and subbed!
Thank you!
Great video.
Thanks!
You are most welcome. Glad you liked the video.
simple wow!
This is great!
Hi Stewart,
Thank you for your clear and concise explanation i recently started iOS Development so i had a doubt regarding threads as you said API calls run on a background thread and UI-related work should be done on main thread.
In my case, I am fetching data from an endpoint using alamofire and wanted to use completion handlers but I am somewhat confused ...in your video you intentionally made your function run in the background thread and then called a specific block of code on the main thread I wanted to ask in my API call do I have to do the same? making the request in the background thread explicitly or it is done automatically?
Apologies for my bad English
Network calls with Alamo fire are always executed on a background thread automatically. I just released a new video today on this topic that may add some clarification too.
Amazing tutorial, thankyou! Can you please tell me what extension (or whatever this is😅) which allows you to add those blocks with explanations to your playgrounds?
Look in the sources folder for the playground pages.
I downloaded your playgrounds, but the sources folders are empty when I open them in X Code😢@@StewartLynch
Thanks for the answer. But even though I downloaded your playgrounds, the Sources folder is empty😢@@StewartLynch
@@user-up2fz5gz6x Sorry. I thought you were referring to something else in another playground. You are probably referring Takto the Markup that I use in that playground. Take a look at this developer.apple.com/library/archive/documentation/Xcode/Reference/xcode_markup_formatting_ref/
thanks a lot!!🤍@@StewartLynch
Stewart how do you format the titles like that in the playground? Can you point me in the right direction to figure that out. Thanks.
Does this help? www.thomashanning.com/xcode-markup-for-playgrounds/
@@StewartLynch Wonderful thanks Stewart!
Thank you!