![]() ![]() Here's an example: let url = URL(string: "") ![]() dataTasks also use completion handlers as part of their initialization. ![]() Giving information to dataTask is called initialization. You can give the dataTask different types of information to help you achieve your goal. Every time you need some data, write to the boss and use. Her job is to share the work with the people in her team, and they'll get the job done. The manager doesn't do anything on her own. URLSession is like the manager of a team. The first example is setting up the API call itself: func fetchFilms(completionHandler: () -> Void) Implementing a function with a completion handler URLSession Here's an example of what a completion handler looks like. Usually what you'll do is reload the table view so the data appears to the user. That way, when your app gets tapped on the shoulder, it can take the information from the completion handler and do something with it. You can specify what that information is. You tell the completion handler to tap your app on the shoulder once it has the information you want. This means you can tell your app to go off and do other things, such as loading the rest of the page. Instead of making your user wait patiently for the server to give you the data, you use a completion handler. You hope the server returns the data quickly, but you don't know how long it will take. You send a URL request to a server, asking it for some data. When Pip taps on you on the shoulder with the answer, you can take her answer and use it. That way you can go about your life, run some errands, read a book, and watch TV. So you ask Pip to tap you on the shoulder when she's ready to give you the answer. You don't know how long, and you don't want to pause your life waiting for it to finish. You use a completion handler in a function when you know that function will take a while to complete. I have no idea how long you'll need to wait on hold, but I'll call you back when we're ready for you." It wouldn't have been as funny, but Pip is offering to be a completion handler for Pheobe. Remember the episode of Friends where Pheobe is glued to the phone for days waiting to speak with customer service? Imagine if at the very start of that phone call, a lovely person called Pip said: "Thanks for calling. Apologies if I misrepresent some concepts. You can see my full project code on GitHub.ĭisclaimer alert: I am new to coding and am largely self-taught. I'll be using the open source Star Wars API to build this project. We'll cover these concepts first, as they are important parts to understanding how to make an API call.įinally we'll put it all together. ![]() Let's go through some building blocks that are often used to make an API call. Now the Swift way is much nicer out of the box, so there's no reason to download a pod. Previously most people used pods like Alamofire and SwiftyJson (you can read about how to do that here). Since the introduction of Codable in Swift 4, making API calls is much easier. The third point is what we'll cover in this article. Third, it's great if you can fetch data from an API and use this data in your table view. Second, you should know how to populate those table views with data. First, it's important to be familiar with creating table views. If you are looking to become an iOS developer, there are some fundamental skills worth knowing. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |