The course is part of this learning path
This course explores the concepts of threading, async, and await in iOS and how you can employ them in your app builds.
Learning Objectives
- Understand how to implement threading (aka concurrency) in your iOS apps
- Learn about the concepts of async image and await and how to use them
Intended Audience
This course is intended for anyone who wants to learn how to develop apps on iOS.
Prerequisites
To get the most out of this course, some basic knowledge of iOS development would be beneficial. We recommend that you take this course as part of the Developing Mobile Apps for iOS learning path.
Hi, within this lecture we're going to start and write the crypto crazy application that we have written before with the MVVM structure one more time. But this time we're going to use Swift UI rather than storyboard and this will give us opportunity to actually practice what we have learned in the Swift UI in the MVVM and also we're going to learn much more things regarding to swift UI and also combine framework. So, let me show you what I mean. I'm just going to create an app. Okay, of course you need X core 13 and above in order to complete this exercise. So, I'm going call this CryptoCrazySwiftUI or something like that, okay? And don't forget to choose Swift UI from the interface and also Swift as a language obviously. Now we don't need any core data or tests. I'm just going to go ahead and say next, and then I'm going to place it on my desktop and here you go. Of course we're writing the same application that we have written before. So, I really suggest you guys go back and watch it if you haven't done so yet. And then later on we're going to change the codes a little bit to introduce the newly concepts like async and await like we have talked before. So, maybe you remember this code. So, let me just go to the nomics.com. So, if you have watched the MVVM section, then you obviously remember this. So, you don't need to do any of this. So, I'm just going to go into the Code Beautify in order to show you how our Json data will look like, okay? So, you know this as I said before and we have worked with nomics.com in order to get the real time Cryptocurrency data before, right? So, we can get all the datas from here with creating an API key and stuff. However, in order not to create many more API keys or just sign up with the website. I just put an exemplary data directly taken from the nomics to my Github. So, I can just send a request to this Github account or Github address in order to get this data, okay? Of course if you want you can create nomics API key and just get it from there like we did before. However maybe you don't want to deal with that. So, I just put the data to my Github account so you can just send the request to it. Over here you can see that it sends the currency and the price of that related currency back to us. So, we have many, many currencies and many prices over here as you can see. So, this is ANTR I don't know what it is. So, let me see this is B2BCOIN. We have B2B, B2C. So, we can see the currencies and the prices of course they are not up to date but you can just get up to date datas from nomics if you want. We have seen how to do that before. And I plan to include this in the resources of this lecture, this URL, so you will be able to reach it. Anyway, I believe we have to go ahead and start coding. We're not going to be dealing with the UI most of the time we're going to focus on the data parts and learn something new that we have never learned before during the course. And in order to do that I'm going to start with writing our model. So, if we're going to stick to the MVVM we have to have a model, a ViewModel, a View, like a web service and we can create different packages different groups for all of the Swift files and then we can just go ahead and do what we want to do. Okay, and it's wise to start with the model because we will need the model in every case like in web service, in ViewModel and stuff. So, I'm going to create a new group over here and call it model and I'm going to create another group. So, this can be View, okay? Let's go for new group and just call this like ViewModel, okay? And then finally let me think. we can create another group and call it service. So, we can write our web service over there with the manulization or whether we want to use it. So, here you go. So, we have Service, ViewModel, View and Model. And for this app, so this is the application itself. It actually defines the content view or the main view that will be shown once the application starts. So, I'm going to leave it there but I'm going to change the content View's name, so I'm going to say right click and 'refactor' and 'rename'. And I'm going to call this something like MainView. Of course we don't have to do that, but content view is not the appropriate name in my opinion. So, here you go, now we have the main view. We're going to do everything that we need to do under the struct. So, I believe we can just move it under View group, okay? So, we can leave the Crypto Crazy UI app over here or you can just move it to the View as well. So, I believe that's okay. Now I'm going to go into the model and say new file and just choose the Swift file and name it something like CryptoCurrency, okay. Whatever you may want to call your model. So, in my case I'm going to name it CryptoCurrency and of course this will be a struct. So, struct CryptoCurrency. Let me write it CryptoCurrency like this and this will be like a Decodable and also identifiable. Great, now decodable and identifiable and you know what it means right? We're going to decode the Json data using this struct. So, that's why it's decodable. So, if I want to just show it inside of a list then it should be an identifiable as well. So, we need two things. The currency as a string and price as a string as well. How do I know that? Because I know the Json data, right? And since it's identifiable, I will also need a UUID or any ID as well. So, in order to do that I can just come over here and say ID = UUID, okay? It will just assign a UUID to each struct object that we create out of this. Great. Now we do that. However if you remember the Json data, it doesn't actually include any ID, right? So, if we come back to the Json data, we know that it actually consists of currency and price only. It doesn't have any ID's. So, it can be a problem for us when we try to decode this, okay? Because if you remember this is decodable. Remember what is decodable. So, this struct, the whole struct is decodable. So, we can decode the Json data according to this struct and we'll just try to match the properties that we create with the Json data itself. So, we need the currency and we need the price. And because, since this is identifiable but we need the ID as well. However, we don't have ID inside of the Json data. So, how do we deal with this? So, what we're going to do, we're going to introduce decoding keys concept in order to match the incoming data with this structure. And to be frank, we named this currency and price exactly as it is. Exactly like we have seen in the data in the Json data but it shouldn't be the case. It's not always the case, right? It could have something like this like it could have the name of something like this rather than a regular currency. So, let me show you what I mean and you will understand it in a better way. So, I'm going to create a private enum and I'm going to name it coding keys and this will be a string and coding key, okay? So, this coding key is a protocol and you need to actually implement this protocol over here. The coding key protocol. The coding keys private enum doesn't actually matter. You can just call it something else but you need to, you need to implement the exact coding key in order to make sure that we're going to supply the coding keys that will match with the incoming Json data and you will see what it means in a minute. Don't worry about it. What we're going to do? We're going to say the Json data, will have these names and I want you match these names with the appropriate variable names. So, what do I mean by that? What is a coding key? Let me write it. So, since this is an enum we're going to write it with case. So I'm going to say, case currency then we're going to look for currency. If it had something else over here then I would have just put it over there like something like that. If it had like r123 inside of it, then I could have done this. Okay, it will just match the currency with this but in my case it's just the currency, okay? And I can just write case price = price as well. So, in this case they are very appropriate. However what we have solved by doing this is that we don't need to worry about ID anymore, okay? Because we haven't actually put it under the coding keys. It's not going to look for the ID. But it's going to assign the ID once we create an object out of this struct. So, this is exactly what I need. Okay, it's all it's going to be decodable, it's going to decode the thing and it's going to be identifiable and also you can see in some resources that it can be hashable as well. So, you can just put the hashable here and won't hurt us. Right, now struct is ready, our model is ready and we have done this before. Okay. So, we went over the concepts one more time and we have seen how to deal with these cases, such as ID's and coding keys and stuff. But this must be pretty easy for you guys at this point, but right now, if I managed to get this data from the internet, then I will be ready to decode it. And that's exactly what we're going to do, right? We're going to create a web service in order to download this from the internet and decode it with our struct. And we know how to do that with data shared or shared data task. We're going to do that within the next lecture together.
Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.