1 00:00:00,390 --> 00:00:06,900 So far when we talk about Jason what we have been able to do is to get agents and file input in our 2 00:00:06,900 --> 00:00:10,500 projects and then pass it that way. 3 00:00:10,500 --> 00:00:17,400 That works fine but most of the Times Jason as we said before we talked about Jason API is intention 4 00:00:17,460 --> 00:00:24,660 usually is to have an API application programming interface essentially is just a way in which US developers 5 00:00:24,660 --> 00:00:30,520 can communicate to a web server which will be serving as some data from database somewhere. 6 00:00:31,020 --> 00:00:37,050 So usually it's in form of an API application programming interface that allows us to get that data 7 00:00:37,350 --> 00:00:44,130 as opposed to having to rely on getting the actual hard code file and put in our projects because that's 8 00:00:44,130 --> 00:00:47,030 just not always the case and it shouldn't be. 9 00:00:47,060 --> 00:00:52,800 OK so how do we then parse data that it's coming from an API. 10 00:00:53,400 --> 00:00:55,660 Well we're going to go ahead and create a new project. 11 00:00:55,710 --> 00:00:56,990 I kind of have a head start. 12 00:00:57,330 --> 00:00:58,670 I'm gonna go ahead and say next. 13 00:00:58,680 --> 00:01:00,180 So I'm going to give it a name. 14 00:01:00,210 --> 00:01:01,610 I'm going to call this pass. 15 00:01:01,840 --> 00:01:04,470 J Just like that next. 16 00:01:05,460 --> 00:01:08,070 And we know exactly where it's supposed to be. 17 00:01:08,070 --> 00:01:12,050 And while I should have a new project looks real good. 18 00:01:12,060 --> 00:01:14,270 So what is it that we're gonna be passing. 19 00:01:14,290 --> 00:01:18,180 Well so there is this website called Jason Place holder. 20 00:01:18,230 --> 00:01:22,820 As you can see here where it gives us a very simple API. 21 00:01:22,820 --> 00:01:25,420 In fact they call it a fake API. 22 00:01:25,500 --> 00:01:26,660 Jason bozo holder. 23 00:01:26,820 --> 00:01:30,240 It's a fake API that allows you to just play around with. 24 00:01:30,390 --> 00:01:33,720 Jason now there are different end points here. 25 00:01:33,720 --> 00:01:40,770 The first endpoint endpoint meaning a place where we can actually go and fetch Jason code and you can 26 00:01:40,770 --> 00:01:44,980 come down here you can see that there is an example you can click try. 27 00:01:45,150 --> 00:01:51,240 And it shows us the actual payload payload in this case suggests the adjacent data. 28 00:01:51,240 --> 00:01:58,350 Now I have to remind you that this should this one remind you of swift dictionary as we've talked before. 29 00:01:58,350 --> 00:02:02,210 If you have been following us so this will come in handy. 30 00:02:02,220 --> 00:02:10,590 Now one thing to remember is that the way the route of our Jason is constructed is very important because 31 00:02:10,710 --> 00:02:11,850 we will see later. 32 00:02:11,910 --> 00:02:13,660 Why is that so. 33 00:02:13,830 --> 00:02:14,850 What is the route. 34 00:02:14,940 --> 00:02:19,300 The route is always what initiates our API. 35 00:02:19,410 --> 00:02:24,810 What I mean by that is that while in this case we know that the route is going to be a dictionary because 36 00:02:24,810 --> 00:02:27,270 we have these curly braces. 37 00:02:27,540 --> 00:02:31,620 So if we go back down here we can see other and points of our API. 38 00:02:31,620 --> 00:02:38,340 For instance if I go to comments you notice right away we see that we have a different structure. 39 00:02:38,340 --> 00:02:42,660 So at this point here as you see that we have what we call an array let's make this a little bit bigger 40 00:02:42,660 --> 00:02:43,390 perhaps. 41 00:02:43,560 --> 00:02:46,780 So you can see there we go. 42 00:02:46,830 --> 00:02:49,700 So we can see it at the top here we have different structure. 43 00:02:49,980 --> 00:02:53,450 So these are brackets which symbolize an array. 44 00:02:53,460 --> 00:02:54,900 This means that our API. 45 00:02:54,900 --> 00:02:57,980 Now the route is indeed in a right. 46 00:02:57,990 --> 00:03:02,270 So this array contains objects or dictionaries. 47 00:03:02,310 --> 00:03:05,460 In fact you can close down like this and open him up. 48 00:03:06,330 --> 00:03:11,230 This will become more clear once we get to passing and so forth. 49 00:03:11,250 --> 00:03:11,550 All right. 50 00:03:11,550 --> 00:03:14,250 So back to our Jason Place holder here. 51 00:03:14,250 --> 00:03:22,470 This is the end point that we're gonna be using to pass of course this OK so let's go ahead and copy 52 00:03:22,470 --> 00:03:25,110 that the euro because we're going to need that. 53 00:03:26,130 --> 00:03:34,200 And let's take us back to our X code so we can go here and say Right click we want to create a new file 54 00:03:35,680 --> 00:03:44,450 let's create a sweet file there and we're going to call this net working manager such. 55 00:03:44,500 --> 00:03:45,410 All right so we have it. 56 00:03:45,420 --> 00:03:48,850 And get rid of all of this stuff that we don't really need. 57 00:03:48,850 --> 00:03:49,140 OK. 58 00:03:49,180 --> 00:03:55,900 So what we're going to do here is we're going to go ahead and create a class called net working manager 59 00:03:56,580 --> 00:04:02,410 and this class here will have to confirm to an observable object because we want to make sure that we 60 00:04:02,410 --> 00:04:06,720 have listeners who will be listening to this network manager. 61 00:04:06,720 --> 00:04:12,460 One moment that we get the data all the listeners will be able to know is we'll be listening right to 62 00:04:12,460 --> 00:04:15,010 this object here that hey there is data. 63 00:04:15,010 --> 00:04:20,740 Something is changed and then we gonna go ahead and revamp our UI or this case will draw our user interface 64 00:04:20,740 --> 00:04:21,490 with the new data. 65 00:04:21,930 --> 00:04:22,160 OK. 66 00:04:22,180 --> 00:04:25,510 So the observable objects are going to say observable. 67 00:04:25,870 --> 00:04:31,790 If I can type of course observable object at such close I want it down there. 68 00:04:31,810 --> 00:04:35,050 And so here is where we going to be doing the coding. 69 00:04:35,050 --> 00:04:40,010 Now before we can go forward here we need to go ahead and create a data list entry. 70 00:04:40,030 --> 00:04:48,160 So these data list entry it's going to map our Jason API payload or object with an actual swift y object. 71 00:04:48,520 --> 00:04:49,400 So what I mean by that. 72 00:04:49,420 --> 00:04:51,760 Let's go ahead and create it real quick. 73 00:04:51,790 --> 00:05:00,430 So Nicole this is going to be a swift file and let's call this data list entry. 74 00:05:00,430 --> 00:05:01,340 Very good. 75 00:05:01,360 --> 00:05:04,560 Good call whatever we want but it seems to be good that name there. 76 00:05:04,900 --> 00:05:09,400 So I'm going to go ahead and paste in one of the example of the payload that we have. 77 00:05:09,400 --> 00:05:10,650 I showed you earlier. 78 00:05:10,720 --> 00:05:14,380 So we have the user I.D. we have the I.D. title and body. 79 00:05:14,380 --> 00:05:18,470 So this is where we expecting to receive from our Jason API. 80 00:05:18,730 --> 00:05:20,240 So this model class can create. 81 00:05:20,260 --> 00:05:27,970 We'll have to map all of these properties so that we can then have those as Swift file. 82 00:05:27,970 --> 00:05:32,920 So here I'm going to say struct data list entry. 83 00:05:32,920 --> 00:05:37,870 And this is gonna be decoder ball as well I's identifiable. 84 00:05:37,870 --> 00:05:43,810 So I'm going to confirm to two different protocols here because we're going to need those in a little 85 00:05:43,810 --> 00:05:44,040 bit. 86 00:05:44,600 --> 00:05:45,030 Okay. 87 00:05:45,040 --> 00:05:47,190 Let me close that one down like this. 88 00:05:47,740 --> 00:05:50,230 Okay let's go ahead and close this down. 89 00:05:50,230 --> 00:05:51,910 Let's go ahead and add a few properties here. 90 00:05:51,910 --> 00:05:54,100 So I say var user I.D.. 91 00:05:54,100 --> 00:05:59,740 Notice these properties have to be exactly the same properties as we are expecting. 92 00:05:59,740 --> 00:06:00,430 OK. 93 00:06:00,520 --> 00:06:05,380 So user I.D. is going to be an integer var I.D.. 94 00:06:05,520 --> 00:06:08,020 It's going to be also an integer. 95 00:06:08,020 --> 00:06:14,150 Var title is a string and far body. 96 00:06:14,170 --> 00:06:15,430 So it's going to be a string. 97 00:06:15,430 --> 00:06:16,740 So essentially all of this. 98 00:06:16,740 --> 00:06:19,060 One two three four. 99 00:06:19,530 --> 00:06:20,460 All right. 100 00:06:20,470 --> 00:06:25,660 Now that we have that go back to our network manager here because we will use our data list entry here 101 00:06:25,810 --> 00:06:28,110 to pass the data. 102 00:06:28,120 --> 00:06:28,950 Let's go ahead here. 103 00:06:28,950 --> 00:06:32,080 I'm going to say var data list. 104 00:06:32,230 --> 00:06:37,720 It's going to be expecting of course an array that will contain data list types. 105 00:06:37,720 --> 00:06:38,840 That makes sense. 106 00:06:38,840 --> 00:06:45,250 We're going to use this data type variable here which expects to be a data list entry to actually populate 107 00:06:45,430 --> 00:06:50,020 our data but of course we have to instantiate it here so I'm going to go ahead and put something like 108 00:06:50,020 --> 00:06:50,290 that. 109 00:06:50,770 --> 00:06:51,230 OK. 110 00:06:51,310 --> 00:06:53,430 So we actually creating new empty array. 111 00:06:53,470 --> 00:06:56,800 We expect to actually have a data list entry types. 112 00:06:56,930 --> 00:07:01,960 The thing here to notice is that because this data at least here is going to be dealing with data that 113 00:07:01,960 --> 00:07:09,160 we're getting from our API this is where we are making sure that these variable indeed can listen and 114 00:07:09,190 --> 00:07:10,020 publish. 115 00:07:10,030 --> 00:07:12,760 In this case actually are the events that happened. 116 00:07:12,760 --> 00:07:18,670 So meaning once we get the data and we populate this data least with the information from our API we 117 00:07:18,670 --> 00:07:25,120 want to make sure that the swift UI knows how to publish this event to any other struct or view that 118 00:07:25,120 --> 00:07:31,920 needs this data for that to happen we have to save at and pass published just like that is that at least 119 00:07:31,920 --> 00:07:36,970 it will be able to publish the data and all the changes that happened to this data to anybody else who 120 00:07:36,970 --> 00:07:38,030 is listening. 121 00:07:38,030 --> 00:07:38,270 OK. 122 00:07:38,290 --> 00:07:39,670 So this is good. 123 00:07:39,670 --> 00:07:46,080 Way of data binding if you will so that the views are actually changing as data comes in so that it 124 00:07:46,090 --> 00:07:53,500 changes the views that are listening to our data list here or our network manager as an observable object 125 00:07:53,800 --> 00:07:57,380 they will know to update their user interface with the new data. 126 00:07:58,080 --> 00:07:58,610 OK. 127 00:07:58,750 --> 00:08:00,740 So let's go ahead and do the actual coding. 128 00:08:00,850 --> 00:08:06,550 So what I'm gonna do here I'm going to create any net initialize are here and I'm not gonna pass anything 129 00:08:06,790 --> 00:08:11,830 inside but I'm going to start as a variable as a guard to let to you or L so anytime you are dealing 130 00:08:11,830 --> 00:08:17,980 with or else we need to make sure that our your l is actually guarded if you will. 131 00:08:17,980 --> 00:08:19,570 Just in case things go wrong. 132 00:08:19,570 --> 00:08:24,340 Something I say is equal to your l I'm going to make sure that your out is actually valid when I'm passing 133 00:08:24,550 --> 00:08:26,850 the string here and the URL. 134 00:08:26,860 --> 00:08:35,780 We are going to be hitting in the end point of our API we're going to be getting is here HDP as Jason 135 00:08:35,990 --> 00:08:37,750 Place holder. 136 00:08:38,450 --> 00:08:39,540 OK. 137 00:08:39,710 --> 00:08:44,530 Type code dot com posts all right. 138 00:08:44,800 --> 00:08:46,790 That's where we are getting everything. 139 00:08:46,990 --> 00:08:54,660 In fact if I go again to this link here you can see this is what we are going to be getting right. 140 00:08:55,370 --> 00:09:02,770 All right so if this goes wrong meaning if or Al for some reason is wrong or something didn't go right 141 00:09:03,140 --> 00:09:08,800 I'm gonna go ahead and pass an else here and just say return like that. 142 00:09:08,890 --> 00:09:13,790 So if anything goes wrong we're just gonna ignore everything and move on to the next statement. 143 00:09:13,810 --> 00:09:20,290 So next want to say your RL session such and I'm gonna invoke the shared Singleton in this case here 144 00:09:21,190 --> 00:09:27,520 when I invoke the data task with your rules so I'm gonna pass the URL that we have there. 145 00:09:27,580 --> 00:09:31,780 So these here will actually allow us to do all of the begging passing getting stuff. 146 00:09:32,140 --> 00:09:37,030 So these here what are we doing we are putting everything in the back end not in the main thread so 147 00:09:37,030 --> 00:09:43,290 we can go ahead and hit this you l here API and start the process of extracting information from it. 148 00:09:43,510 --> 00:09:48,060 So then we're going to be expecting a few things from our data task. 149 00:09:48,090 --> 00:09:53,860 So the first one is data which is will contain the payload Chase on persay in a different format and 150 00:09:53,860 --> 00:10:00,400 then we can ignore anything else with underscores like that when I say in some closures and then I'm 151 00:10:00,440 --> 00:10:06,410 say guard again just protect ourselves like data that we're receiving say data. 152 00:10:06,410 --> 00:10:10,520 Else if something goes wrong again when I just say return like that. 153 00:10:10,540 --> 00:10:10,850 Okay. 154 00:10:11,200 --> 00:10:15,530 So in this case here we're going to make sure that if data is actually something usable Of course we're 155 00:10:15,530 --> 00:10:17,670 going to put inside of this variable here. 156 00:10:17,740 --> 00:10:19,770 Else let's just move on. 157 00:10:19,780 --> 00:10:23,000 So now that we have that data I'm going to say let data list. 158 00:10:23,440 --> 00:10:24,140 OK. 159 00:10:24,310 --> 00:10:27,940 I'm going to make it a try here just in case things don't go right. 160 00:10:27,970 --> 00:10:35,860 And then I'm gonna invoke the sun decoder because we need to decode whatever we're getting into something 161 00:10:35,890 --> 00:10:37,660 that makes sense for Swifty y. 162 00:10:37,810 --> 00:10:41,420 So Jason decoder I'm getting to decode method. 163 00:10:41,470 --> 00:10:46,920 Notice 40 decode method we have to pass the actual type that we want to use to decode. 164 00:10:47,290 --> 00:10:53,260 Because remember what we're trying to do is to map our Jason payload the object we are getting with 165 00:10:53,350 --> 00:10:54,730 these data list entry. 166 00:10:54,730 --> 00:10:59,720 That's why we need to pass some sort of the type that we want to map with in this case here. 167 00:10:59,740 --> 00:11:06,070 The type we want to map it with is going to be of course an array that will contain data list entry 168 00:11:06,310 --> 00:11:09,170 has to be exactly the same thing as we did here. 169 00:11:09,180 --> 00:11:09,640 OK. 170 00:11:09,760 --> 00:11:16,080 Well from what it's going to be from our data which is this one that we're passing that's extra spaces 171 00:11:16,090 --> 00:11:21,820 there now to finalize this we have to say that self want to say we want actually to go and get the data 172 00:11:21,820 --> 00:11:24,210 type not the object itself. 173 00:11:24,280 --> 00:11:25,330 Very good. 174 00:11:25,330 --> 00:11:28,040 So once we have that we're going to say dispatch. 175 00:11:28,040 --> 00:11:33,430 Q Now the dispatch queue here allows us to manage the execution of our task. 176 00:11:33,460 --> 00:11:33,870 Right. 177 00:11:33,890 --> 00:11:39,290 Could be serially or concurrently in our apps main thread or in a background thread. 178 00:11:39,290 --> 00:11:43,120 So I'm going to use that for our advantage because I want to make sure that everything happens in the 179 00:11:43,120 --> 00:11:49,720 back and I'm going to go to main at async because I want to make sure that this happens asynchronously 180 00:11:50,170 --> 00:11:52,430 when it passed the first one closure there. 181 00:11:52,600 --> 00:11:59,130 And then in this case here once we have our data we're going to populate our data list that stadium 182 00:11:59,140 --> 00:12:04,450 because now we have the actual data list that we created here which contains everything mapped out for 183 00:12:04,450 --> 00:12:13,030 us to start using in our code the data that is when say self that data list meaning this guy here which 184 00:12:13,030 --> 00:12:14,520 is a published right. 185 00:12:14,740 --> 00:12:21,400 And I'm going to just add what I have to it is going to be equal to data list which is that it lists 186 00:12:21,430 --> 00:12:28,330 is this one because at this point here they said at least has the actual Jason which we decoded to look 187 00:12:28,570 --> 00:12:33,690 like an actual swift object which we can then go and fetch all of the properties. 188 00:12:34,200 --> 00:12:34,840 OK. 189 00:12:34,870 --> 00:12:42,040 And before we forget we actually have to invoke your resume method which resumes the task if God forbid 190 00:12:42,160 --> 00:12:43,770 if it's suspended. 191 00:12:43,770 --> 00:12:44,270 OK. 192 00:12:44,380 --> 00:12:46,950 And here inside here we can use all sorts of things. 193 00:12:46,960 --> 00:12:53,470 I mean just go ahead and print for ourself that data list which is this guy here which of course should 194 00:12:53,470 --> 00:12:58,750 have all the payload meaning again should have all of this. 195 00:12:58,750 --> 00:12:59,800 OK very good. 196 00:12:59,830 --> 00:13:03,850 So if we go ahead and run right now of course nothing is going to happen because we haven't actually 197 00:13:03,850 --> 00:13:08,470 invoked this networking manager which we're going to be doing in the next video. 198 00:13:08,500 --> 00:13:13,510 So everything should at least the work for now and a next video we are going to see how to invoke this 199 00:13:13,510 --> 00:13:17,170 network manager instead of our kind of view so we can hopefully see something.