1
00:00:08,160 --> 00:00:11,180
Hey everybody what's going on this is Caleb with slopes dot com.

2
00:00:11,190 --> 00:00:16,530
And in this video we're going to set up our download service singleton which is going to help us download

3
00:00:16,530 --> 00:00:17,960
data from the get her API.

4
00:00:17,970 --> 00:00:23,430
So go ahead pull open your project and we're going to create a new folder actually we're going to go

5
00:00:23,430 --> 00:00:29,310
ahead and write click on versey app select new group and we're going to call this services because we're

6
00:00:29,310 --> 00:00:33,440
going to put our download service in the services folder it only makes sense.

7
00:00:33,450 --> 00:00:41,010
Also we should create a second folder called utilities and inside utilities is where we're going to

8
00:00:41,010 --> 00:00:48,210
put our constants folder and this Constans folder is going to be used to basically hold variables that

9
00:00:48,210 --> 00:00:55,760
hold reference to certain things that are still very string based like for instance our YOU ARE L's

10
00:00:55,770 --> 00:00:57,590
for our search queries.

11
00:00:57,610 --> 00:01:02,160
K so what we're going to do is we're going to go ahead and actually create that first because we'll

12
00:01:02,160 --> 00:01:04,190
need it when creating our functions.

13
00:01:04,200 --> 00:01:10,690
So go ahead and right click on the utilities group click new file and click swift file.

14
00:01:10,830 --> 00:01:15,460
They call this Constance and press create.

15
00:01:15,530 --> 00:01:21,200
OK so we don't need to create a class or anything but I am going to go ahead and separate these with

16
00:01:21,200 --> 00:01:24,000
a comment and just type Mark dash.

17
00:01:24,140 --> 00:01:26,650
And this is going to be for you or else.

18
00:01:26,930 --> 00:01:27,670
OK.

19
00:01:27,930 --> 00:01:34,760
Now I'm going to create one for the you l needed to search for Trending repositories.

20
00:01:34,780 --> 00:01:35,000
OK.

21
00:01:35,000 --> 00:01:39,630
So go ahead and type Let us trending repo.

22
00:01:39,860 --> 00:01:42,890
You r l and that's going to be a string of course.

23
00:01:43,220 --> 00:01:48,980
And if you remember when we looked at the get hub API it went like this DTP.

24
00:01:49,130 --> 00:01:49,740
Oh my goodness.

25
00:01:49,760 --> 00:01:53,700
H t t p s colon backslash backslash API.

26
00:01:53,750 --> 00:01:57,660
Get hub dot com slash search.

27
00:01:57,720 --> 00:01:59,740
And really I should be copying this from get her.

28
00:01:59,750 --> 00:02:04,910
But anyway repository Torrey's.

29
00:02:05,240 --> 00:02:05,600
OK.

30
00:02:05,600 --> 00:02:07,040
And then we have a question mark.

31
00:02:07,040 --> 00:02:09,470
Q equals and you know what.

32
00:02:09,500 --> 00:02:16,290
Right here this is where your query is going to go so I'm going to say that for now.

33
00:02:16,370 --> 00:02:18,650
And we're going to go ahead and do and sort.

34
00:02:18,950 --> 00:02:19,590
OK.

35
00:02:19,640 --> 00:02:24,320
Remember it allows us to sort it by stars meaning we're going to get the most popular results first

36
00:02:24,920 --> 00:02:29,750
then we can do and well and order equals descending.

37
00:02:29,750 --> 00:02:34,540
That's going to return us the most popular ones first just like that.

38
00:02:34,580 --> 00:02:39,140
Now I chose to go with swift repositories that were popular.

39
00:02:39,240 --> 00:02:43,610
K but you don't have to write you can still you can choose whatever you want.

40
00:02:43,610 --> 00:02:48,170
So I'm just going to go ahead and type Swift because those are the repositories that I was interested

41
00:02:48,170 --> 00:02:48,580
in.

42
00:02:48,620 --> 00:02:53,850
If you're interested in repositories that are written in go you can search for go.

43
00:02:54,140 --> 00:03:00,020
You can search for repositories written in assembly just like the Tetris example from a few videos back.

44
00:03:00,020 --> 00:03:05,780
So this is the you are all we're going to use to download that Jaison data for swift repositories that

45
00:03:05,810 --> 00:03:08,770
are sorted by Sarr stars descending.

46
00:03:09,670 --> 00:03:12,180
I don't know if that's a real word but it is now.

47
00:03:12,200 --> 00:03:17,490
So while we're here we may as well also include a search you are ill.

48
00:03:17,540 --> 00:03:23,450
So go ahead and type let search you are l and this is going to be the U R L we will use when we are

49
00:03:23,720 --> 00:03:29,870
building out our search Visi and it's basically going to be this same exact u r l except we're going

50
00:03:29,870 --> 00:03:34,970
to stop right after the Q and then write a function later that's going to allow people to pass in their

51
00:03:34,970 --> 00:03:38,220
query and will append it to the end.

52
00:03:38,300 --> 00:03:39,200
Pretty cool.

53
00:03:39,440 --> 00:03:40,800
Let's go ahead and let's do that.

54
00:03:40,800 --> 00:03:42,660
So go ahead and just.

55
00:03:42,680 --> 00:03:46,560
Actually we can copy and paste from here like so.

56
00:03:46,970 --> 00:03:47,790
There we go.

57
00:03:47,990 --> 00:03:48,850
And you know what.

58
00:03:48,860 --> 00:03:50,930
Let's actually save this here.

59
00:03:50,930 --> 00:03:55,730
At the end let's just say that is the chunk that we could add in as its own little segment.

60
00:03:55,730 --> 00:04:03,290
So let's just say let stars descending segment

61
00:04:05,740 --> 00:04:07,890
equals and there we go.

62
00:04:07,930 --> 00:04:08,890
Boom.

63
00:04:08,890 --> 00:04:14,590
So this needs to be a string because we want to be able to just tack that on right at the end and that

64
00:04:14,590 --> 00:04:22,540
will allow us to sort it by stars descending Lee and then of course as you remembered in the simulator

65
00:04:22,540 --> 00:04:26,220
when we open our app and we click on the View read me button.

66
00:04:26,260 --> 00:04:33,850
It's supposed to open a Safari Web controller but it's supposed to take you to the read me page.

67
00:04:33,850 --> 00:04:39,970
Now if I get the R L for a certain repository and I want to view the read me there is a segment that

68
00:04:39,970 --> 00:04:41,420
goes after the u r l.

69
00:04:41,470 --> 00:04:44,350
Every single time and that makes it easy for us to load that up.

70
00:04:44,350 --> 00:04:54,560
So lets just type let read me segment and that is equal to slash blob slash M. slash.

71
00:04:54,610 --> 00:05:03,010
Read me M.D and for every repository it may not be exactly the same and some repositories might not

72
00:05:03,010 --> 00:05:04,130
even have read me.

73
00:05:04,180 --> 00:05:08,170
In that case you'll just get a 404 error on get help when you're looking at it on your phone.

74
00:05:08,170 --> 00:05:12,640
But for the most part I would say 99 percent of the time they're going to have a read me in this exact

75
00:05:12,700 --> 00:05:16,090
same format so that is what you can do.

76
00:05:16,090 --> 00:05:20,680
Now if you wanted to just make this save you repository great they could click on that and it could

77
00:05:20,680 --> 00:05:21,820
just show the repository.

78
00:05:21,820 --> 00:05:25,210
But I think it's cool to be able to view the read me because that's where most of the good information

79
00:05:25,210 --> 00:05:28,090
is anyway so this is great.

80
00:05:28,090 --> 00:05:31,890
These are are you or else we can use this in our download service.

81
00:05:31,900 --> 00:05:34,580
So let's go ahead let's create it now.

82
00:05:34,720 --> 00:05:40,610
Right click on services click new file and we're going to go ahead and just create a swift File.

83
00:05:40,660 --> 00:05:45,020
We're going to call it download service and press create.

84
00:05:45,040 --> 00:05:50,950
Now you've heard me use the word Singleton and a singleton class is basically a class that is going

85
00:05:50,950 --> 00:05:58,630
to be publicly accessible by any and all classes in our app for the entire lifetime of the application.

86
00:05:58,630 --> 00:06:01,010
So as long as it's running that class is available.

87
00:06:01,240 --> 00:06:05,910
So we're going to actually create it now by typing class download service.

88
00:06:06,350 --> 00:06:06,830
OK.

89
00:06:06,850 --> 00:06:12,760
And in order to create this into a singleton class we need to create an instance of this class inside

90
00:06:12,760 --> 00:06:17,410
itself using what's called a static variable or static constant.

91
00:06:17,410 --> 00:06:24,280
So go ahead and type let instance wups instance equals download service and instantiate it.

92
00:06:24,370 --> 00:06:24,580
Right.

93
00:06:24,580 --> 00:06:28,160
We're creating an instance of this class inside itself.

94
00:06:28,300 --> 00:06:33,880
But if we want it to be accessible for the entire lifetime of this application we need to start it with

95
00:06:33,880 --> 00:06:34,760
static.

96
00:06:35,050 --> 00:06:37,000
OK so it's a static constant.

97
00:06:37,150 --> 00:06:43,010
And I don't know if it'll actually give you information on why we're calling this static here but any

98
00:06:43,020 --> 00:06:43,750
way you can look it up.

99
00:06:43,750 --> 00:06:50,200
Basically what static means is it turns this instance into an instance that is going to stay in memory

100
00:06:50,470 --> 00:06:52,830
for the entire lifetime of the application.

101
00:06:52,840 --> 00:06:58,450
So now if we wanted to access this from anywhere we could just call a download service instance and

102
00:06:58,450 --> 00:07:01,210
we have access to everything that's going on in this class.

103
00:07:01,210 --> 00:07:02,680
Really really cool.

104
00:07:02,680 --> 00:07:06,740
Of course you don't want to abuse the singleton pattern it is possible and people do it.

105
00:07:06,760 --> 00:07:12,640
So let's go ahead and let's start by writing our first function and we're going to actually call it

106
00:07:12,730 --> 00:07:16,580
download trending repo's dictionaries array.

107
00:07:16,580 --> 00:07:19,590
It's a really long name but you'll see why in a second.

108
00:07:19,630 --> 00:07:29,050
So go ahead and type phunk download whip's rending repo's dictionary array.

109
00:07:29,050 --> 00:07:29,890
All right.

110
00:07:30,070 --> 00:07:37,150
That's a really long name but basically the Jason that is downloaded in is an array that is full of

111
00:07:37,150 --> 00:07:42,880
dictionaries each repo comes in as its own dictionary and the Jaison that comes in basically parses

112
00:07:42,880 --> 00:07:48,430
it into an array that's full of dictionaries so we can access a dictionary at a certain index in the

113
00:07:48,430 --> 00:07:50,040
array which is pretty cool.

114
00:07:50,050 --> 00:07:54,340
It's a little annoying to have to cycle through all of it but I'll show you how we do it.

115
00:07:54,340 --> 00:08:00,010
So in order to actually return values we're going to be using a lot of nested closures.

116
00:08:00,100 --> 00:08:04,300
And since we're using closures we're going to go ahead and need to use a completion handler to return

117
00:08:04,300 --> 00:08:05,620
the values.

118
00:08:05,620 --> 00:08:10,170
So go ahead and type completion and it's going to be escaping.

119
00:08:10,180 --> 00:08:16,600
And if you don't know what escaping means basically if you have an escaping closure it allows for values

120
00:08:16,600 --> 00:08:22,500
to be passed in and then they can actually escape out of the closure to wherever this function is called.

121
00:08:22,510 --> 00:08:26,820
It's sort of like returning a value to a function except it lets us do it out of a closure.

122
00:08:27,100 --> 00:08:32,920
So for this escaping closure we need to actually pass it a function and it's going to just return an

123
00:08:32,950 --> 00:08:36,720
empty function because we don't care about the return necessarily.

124
00:08:36,860 --> 00:08:41,230
We do care about what gets passed into this function because basically think of this function like a

125
00:08:41,310 --> 00:08:42,070
like a black hole.

126
00:08:42,070 --> 00:08:46,360
We can pass the values in through it and then on the other side wherever this function is called we

127
00:08:46,360 --> 00:08:48,770
can access those values.

128
00:08:48,850 --> 00:08:55,610
So we're going to go ahead and use repots dictionary array like so.

129
00:08:55,900 --> 00:09:01,900
And just like that it's going to be an array like we said of dictionary and the dictionary is going

130
00:09:01,900 --> 00:09:09,380
to be of type string and any it could come in as a string could come in as an integer any value so we

131
00:09:09,380 --> 00:09:12,290
don't want to have a crash just because we get a value that we don't like.

132
00:09:12,290 --> 00:09:14,580
So any is the way to do it.

133
00:09:14,600 --> 00:09:19,340
Now we're getting an error and it's saying function types cannot have argument labels use underscore

134
00:09:19,460 --> 00:09:20,850
before repots dict array.

135
00:09:21,000 --> 00:09:25,130
OK so we can't use a name for whatever reason Swift doesn't like it.

136
00:09:25,130 --> 00:09:31,630
So we're just going to use an underscore just so that we can see this internal parameter but you know

137
00:09:31,640 --> 00:09:34,940
switch is not going to let us use that as a parameter for whatever reason.

138
00:09:35,180 --> 00:09:37,230
We'll have to ask the swim team why that is.

139
00:09:37,250 --> 00:09:44,150
But anyway so now what we can do is we can start doing our downloading which is really exciting and

140
00:09:44,150 --> 00:09:46,520
to do that we're going to need to use Alamo fire.

141
00:09:46,580 --> 00:09:48,280
That's one of the libraries that we installed.

142
00:09:48,290 --> 00:09:52,310
So go ahead and type in port Alamo fire.

143
00:09:52,310 --> 00:09:57,880
There we go and Alamo fire is going to help us to make network request and download stuff really easily.

144
00:09:57,920 --> 00:09:59,250
So let's do it.

145
00:09:59,480 --> 00:10:03,830
Let's go ahead and type Alino fire request.

146
00:10:03,980 --> 00:10:09,320
And as you can tell we can pass in a convertible which can come in as a string which is really cool.

147
00:10:09,350 --> 00:10:15,810
So go ahead and type daat request and for the you Arel we can pass in our constant trending repo.

148
00:10:15,860 --> 00:10:19,280
You r l we just created that and our Constans file appear.

149
00:10:19,280 --> 00:10:23,570
So pass it in and then basically we are making a request.

150
00:10:23,600 --> 00:10:26,920
Now we need to think about what are we going to do with that request.

151
00:10:26,930 --> 00:10:28,510
We want it to come in as Jaison.

152
00:10:28,520 --> 00:10:31,390
But watch what happens if I type dot response.

153
00:10:31,520 --> 00:10:37,630
You can see we can get it as a response with the completion handler response as just raw data response

154
00:10:37,670 --> 00:10:41,840
as Jason ding ding ding response string response property property list.

155
00:10:41,840 --> 00:10:47,420
We can get a lot of different types but we want Jason so go ahead and click on Jason and then for the

156
00:10:47,420 --> 00:10:52,520
completion handler we just need to press enter and the property here this is the response.

157
00:10:52,520 --> 00:10:54,950
This is the Jason data we're gonna get back.

158
00:10:54,950 --> 00:10:58,530
So I'm just going to go ahead and just call that response.

159
00:10:58,670 --> 00:11:03,470
That's just the property name I'm going to use for it and we're going to basically now create a constant

160
00:11:03,530 --> 00:11:08,380
called Jaison that is going to be set to the value of whatever response this is.

161
00:11:08,390 --> 00:11:14,360
So let's go ahead and let's use guard Latt because when dealing with network quest you want to be extra

162
00:11:14,360 --> 00:11:18,280
extra safe because not all repositories are going to be the same.

163
00:11:18,290 --> 00:11:21,920
Some may have some data that another does not.

164
00:11:21,920 --> 00:11:27,140
Each repository is going to have required data but some might have more than another and in order to

165
00:11:27,140 --> 00:11:32,570
avoid crashes we want to make sure that we are optionally casting things when we can just to make sure

166
00:11:32,570 --> 00:11:33,080
it's safe.

167
00:11:33,080 --> 00:11:40,300
So go ahead and type guard let Jason and Jason is going to be equal to response.

168
00:11:40,820 --> 00:11:41,290
Oh you know what.

169
00:11:41,290 --> 00:11:43,190
I spelled that wrong response.

170
00:11:43,190 --> 00:11:44,120
There we go.

171
00:11:44,150 --> 00:11:45,880
Response.

172
00:11:46,010 --> 00:11:47,170
Result.

173
00:11:47,630 --> 00:11:52,490
OK and then from the result we're going to pull out the value k which that's going to bring that wrod

174
00:11:52,490 --> 00:11:53,680
Jaison data.

175
00:11:53,780 --> 00:11:55,970
So we're going to need to cast this.

176
00:11:55,970 --> 00:12:01,970
Like I told you as a dictionary of string.

177
00:12:02,450 --> 00:12:03,540
And any.

178
00:12:03,550 --> 00:12:04,070
OK.

179
00:12:04,280 --> 00:12:09,220
Now since we're using guard let We need to give another condition in case we cannot get that value.

180
00:12:09,220 --> 00:12:14,570
We're going to type else and then we're just going to return because this function does not require

181
00:12:14,570 --> 00:12:15,950
us to return anything.

182
00:12:15,950 --> 00:12:19,730
So we're just going to go ahead and return get out of the function and keep us from crashing.

183
00:12:19,730 --> 00:12:24,130
Now you're wondering why is this coming in as a dictionary of string.

184
00:12:24,230 --> 00:12:25,720
And any and you know what.

185
00:12:25,730 --> 00:12:30,630
If you remember when we made a call to the API and let me pull that up here.

186
00:12:30,710 --> 00:12:31,280
So here you go.

187
00:12:31,280 --> 00:12:37,790
As you can see when we make a call to the API it actually comes in as a dictionary first I think I misspoke

188
00:12:37,790 --> 00:12:38,510
earlier.

189
00:12:38,510 --> 00:12:40,100
It comes in as a dictionary.

190
00:12:40,100 --> 00:12:47,510
But what we need the important stuff is in items and items happens to be an array of dictionaries.

191
00:12:47,510 --> 00:12:51,550
There's one dictionary and later there's another dictionary.

192
00:12:51,800 --> 00:12:57,050
And so it's in a dictionary that contains an array of dictionaries so as you can see it can get kind

193
00:12:57,050 --> 00:13:04,610
of complex kind of hairy fast so we're going to go ahead and just cast the initial Jaison library as

194
00:13:06,230 --> 00:13:12,800
just a dictionary of string and any Then later we'll pull out the individual array that's full of dictionaries.

195
00:13:12,920 --> 00:13:14,640
But for now let's just see how we did.

196
00:13:14,660 --> 00:13:16,980
Let's go ahead and print.

197
00:13:17,180 --> 00:13:23,170
Print the Jaison and this will just basically let us see if we're downloading the data properly.

198
00:13:23,180 --> 00:13:25,620
But we need to actually call this from somewhere.

199
00:13:25,700 --> 00:13:26,470
Right.

200
00:13:26,480 --> 00:13:31,720
So let's go ahead and let's just go to trending feed Visi and when the view loads we'll just call a

201
00:13:31,720 --> 00:13:36,080
download service instance that's how you access that singleton class.

202
00:13:36,380 --> 00:13:40,160
And then we can call download trending repo's dict array.

203
00:13:40,400 --> 00:13:42,260
On completion we'll just.

204
00:13:42,410 --> 00:13:42,980
Well you know what.

205
00:13:43,000 --> 00:13:45,600
It's actually going to be returning something to us.

206
00:13:46,130 --> 00:13:50,540
Let's just go ahead and just say Jason guys we're going to delete this and fix this in a second we'll

207
00:13:50,540 --> 00:13:57,340
just print done downloading and now we're calling it which is good.

208
00:13:57,350 --> 00:14:02,000
We'll delete it in a second but let's just go check to see if we're actually successfully downloading

209
00:14:02,000 --> 00:14:03,290
all of our Jason data.

210
00:14:03,500 --> 00:14:05,080
And this is the hardest part guys.

211
00:14:05,110 --> 00:14:10,630
After this it's basically just writing little functions to do the custom stuff that we want to do.

212
00:14:10,810 --> 00:14:12,340
And hey look at that.

213
00:14:12,340 --> 00:14:15,980
I see Jason there's all of our Jason data from the API.

214
00:14:16,000 --> 00:14:20,030
Obviously that's way a way way way more than we want.

215
00:14:20,080 --> 00:14:21,160
But check it out.

216
00:14:21,160 --> 00:14:24,470
So we're getting an array here and we're printing out our Jason.

217
00:14:24,490 --> 00:14:28,570
There's the first dictionary all of the you are Elle's the fork's count.

218
00:14:28,570 --> 00:14:30,020
We get the full name.

219
00:14:30,160 --> 00:14:31,560
We get all that cool data.

220
00:14:31,570 --> 00:14:38,150
That's exactly what we want and you notice we're not yet getting this done downloading printing out.

221
00:14:38,380 --> 00:14:43,960
That's because we never called our completion handler so we're not handling completion but we are definitely

222
00:14:43,960 --> 00:14:45,030
on the right track.

223
00:14:45,100 --> 00:14:49,930
We're downloading all of our Jason data and now it's just a matter of parsing through it and creating

224
00:14:49,930 --> 00:14:51,550
the proper repositories that we need.

225
00:14:51,550 --> 00:14:55,990
So let's go ahead and let's actually close the console there.

226
00:14:56,050 --> 00:14:59,390
Stop the app from running and let's go ahead and clean up what we just did.

227
00:14:59,440 --> 00:15:01,990
We don't want to call that because we don't need to.

228
00:15:02,380 --> 00:15:04,300
But guys that is awesome.

229
00:15:04,300 --> 00:15:07,050
So we are getting our Jason data now.

230
00:15:07,090 --> 00:15:10,170
We should basically pull out that item's array of dictionaries.

231
00:15:10,170 --> 00:15:13,330
So let's go ahead and let's create another guard to do that.

232
00:15:13,330 --> 00:15:21,520
So type guard let repo dictionary array because the Jason is not only repo dictionaries.

233
00:15:21,790 --> 00:15:26,310
We're going to go ahead and set dictionary array to be equal to Jaison.

234
00:15:26,320 --> 00:15:27,760
Now remember it's a dictionary.

235
00:15:27,760 --> 00:15:31,040
So in order to get the value we need to pass in the key.

236
00:15:31,150 --> 00:15:33,170
So go ahead and put some brackets there.

237
00:15:33,220 --> 00:15:36,210
And the key as you know is items right.

238
00:15:36,220 --> 00:15:42,580
We saw that and what we can do is we need to actually cast this as the type we want because we know

239
00:15:42,580 --> 00:15:49,460
that items is an array of dictionaries so we can cast it like so array dictionary.

240
00:15:49,690 --> 00:15:56,750
And it is an array of string whip's string and any K just like that.

241
00:15:56,860 --> 00:15:58,220
But we are using a guard.

242
00:15:58,300 --> 00:16:03,790
So we need to provide another condition if we can't do that then we're just going to go ahead and return

243
00:16:04,190 --> 00:16:07,750
K and it is just common to put two spaces on either side.

244
00:16:07,810 --> 00:16:09,870
But now we have a repo dictionary.

245
00:16:10,090 --> 00:16:15,790
Now we're not doing anything with it but we are getting an array of dictionaries.

246
00:16:15,850 --> 00:16:17,860
That is exactly what we wanted right.

247
00:16:17,860 --> 00:16:20,810
We have our training repo's dictionary array.

248
00:16:20,950 --> 00:16:27,040
So we now have the proper repository we have it exactly how we want it.

249
00:16:27,040 --> 00:16:32,850
Now all we need to do is somehow find a way to pass it back to our function.

250
00:16:32,860 --> 00:16:35,520
Right we need to pass it back to our completion handler.

251
00:16:35,530 --> 00:16:41,530
So what we're actually going to do is create a variable at the top of this class and then at the bottom

252
00:16:41,770 --> 00:16:47,260
after we have created this dictionary array we're going to pass it in to our completion handler so that

253
00:16:47,260 --> 00:16:49,210
when we call the function we can access it.

254
00:16:49,210 --> 00:16:56,830
So let's create that variable now go ahead and type trending repots array and it's going to be an array

255
00:16:57,040 --> 00:17:04,600
of dictionary of type string and string and any K.

256
00:17:04,600 --> 00:17:08,600
And we're going to go ahead and instantiate it from the get go like that.

257
00:17:08,950 --> 00:17:15,070
And now we have an array that we can set the value and we just created that right here right.

258
00:17:15,070 --> 00:17:19,200
So what we can do is we can go ahead and set trending repots array.

259
00:17:19,540 --> 00:17:22,750
And that's going to be equal to repot dictionary right.

260
00:17:22,840 --> 00:17:24,620
Pretty easy right.

261
00:17:24,670 --> 00:17:30,740
So this is great and all but we need to think we're actually returning like all 800 values.

262
00:17:30,910 --> 00:17:34,450
All eight hundred little dictionaries in here and I don't think we want that many.

263
00:17:34,450 --> 00:17:37,940
Maybe we should just display like the top 10 values.

264
00:17:37,960 --> 00:17:38,570
OK.

265
00:17:38,770 --> 00:17:44,320
So let's go ahead and actually get rid of this first and we can cycle through our repo dictionary array.

266
00:17:44,410 --> 00:17:49,450
And let's just keep track of how many items are in this array and once we reach a certain number then

267
00:17:49,450 --> 00:17:50,260
we'll break.

268
00:17:50,260 --> 00:17:57,730
So let's just say if we all know let's go through the array first four repo dictionary in Ribault dictionary

269
00:17:57,730 --> 00:17:58,720
array.

270
00:17:59,290 --> 00:18:08,110
And if the trending repots array count is less than or equal to 10 k so we're going to just say we're

271
00:18:08,110 --> 00:18:09,870
going to stop at 10.

272
00:18:09,880 --> 00:18:15,850
Let's go ahead and let's just say trending repots array dot append and we're going to append the repo

273
00:18:15,850 --> 00:18:16,670
dictionary.

274
00:18:16,870 --> 00:18:17,720
OK.

275
00:18:18,100 --> 00:18:19,940
So that way it will stop.

276
00:18:19,960 --> 00:18:25,820
But let's decide what it does so if it gets to 10 it's going to append.

277
00:18:25,840 --> 00:18:31,060
And then once it's over 10 it's not going to append anymore so we need to say else and we need to break

278
00:18:31,060 --> 00:18:34,520
out of the function basically just stopping the for loop from running.

279
00:18:34,540 --> 00:18:36,680
Oh sorry I didn't break out of the for loop.

280
00:18:36,730 --> 00:18:38,750
Stopping it from doing its thing.

281
00:18:38,950 --> 00:18:45,450
Then at that point after that for loop is finished once we are completely done with our Alamo fire call

282
00:18:45,460 --> 00:18:47,010
downloading all of our data.

283
00:18:47,050 --> 00:18:51,820
We are done what we can do is at the bottom call completion.

284
00:18:51,820 --> 00:18:57,910
And then inside of our function look at that it's asking for an array of dictionaries we can pass it

285
00:18:58,240 --> 00:19:02,200
trending repots array because we have filled it with 10 items.

286
00:19:02,220 --> 00:19:03,810
Kate that's super easy.

287
00:19:03,840 --> 00:19:10,740
Now let's go to read and let's call it again let's see if we get 10 repo's and let's go ahead and just

288
00:19:10,740 --> 00:19:17,100
double check that everything works so call download service from trending feed VCs view and load instance

289
00:19:17,700 --> 00:19:20,100
download trending repots dict array.

290
00:19:20,280 --> 00:19:24,900
And we're just going to call this repots dict array.

291
00:19:25,430 --> 00:19:25,740
All right.

292
00:19:25,740 --> 00:19:28,000
And let's just say print.

293
00:19:28,020 --> 00:19:31,170
We're going to print out the repo's directory.

294
00:19:31,590 --> 00:19:32,250
Sounds good.

295
00:19:32,250 --> 00:19:36,400
There we go and as soon as it's done downloading it should print right here.

296
00:19:36,720 --> 00:19:38,010
OK let's check it out.

297
00:19:38,010 --> 00:19:42,290
The app is loading the console is coming up.

298
00:19:43,060 --> 00:19:44,130
Are right.

299
00:19:44,210 --> 00:19:48,890
So it looks like we're getting nothing looked at it printed out an empty array.

300
00:19:48,890 --> 00:19:49,870
So that's not good.

301
00:19:49,940 --> 00:19:52,970
Let's go look to see what the problem is.

302
00:19:53,030 --> 00:20:02,190
So it appears that we are cycling through K we go through here for Ribault DECT in repo dictionary array.

303
00:20:02,390 --> 00:20:06,050
If the count is less than or equal to 10 which it starts out as zero.

304
00:20:06,050 --> 00:20:07,160
So of course it is.

305
00:20:07,280 --> 00:20:18,630
We append it and then we go through again we we appendant go through again until it gets to 10 and then

306
00:20:18,690 --> 00:20:22,660
after that we are going to go ahead and call completion.

307
00:20:22,680 --> 00:20:24,160
Oh you know what.

308
00:20:24,480 --> 00:20:26,110
Alamo fire is a closure.

309
00:20:26,190 --> 00:20:30,760
So we we never tell it within the closure that we are finished.

310
00:20:30,840 --> 00:20:33,860
We call it afterwards so that is part of our problem.

311
00:20:34,080 --> 00:20:39,480
Basically we need to still be inside the same closure block to tell it that we're done because it never

312
00:20:39,480 --> 00:20:42,450
knows when we're done because it goes inside this closure and it can't get out.

313
00:20:42,450 --> 00:20:43,510
It's kind of like a trap.

314
00:20:43,590 --> 00:20:44,850
So let's try that again.

315
00:20:44,970 --> 00:20:53,300
Let's build and run and let's see if it'll properly call our function it's loading.

316
00:20:53,560 --> 00:20:54,940
There we go.

317
00:20:54,940 --> 00:21:02,080
So we're getting all the data and it looks like now we only have 10 10 items coming in because let's

318
00:21:02,080 --> 00:21:04,860
see if we have one.

319
00:21:04,940 --> 00:21:10,370
There we go it's hard to find the end of one and the start of another but I can tell for sure this is

320
00:21:10,370 --> 00:21:12,070
a lot less data.

321
00:21:12,140 --> 00:21:17,810
So we're starting with Apple's Swift which is obviously the highest and we are ending with one that

322
00:21:17,810 --> 00:21:22,600
is pretty close but pretty much lower.

323
00:21:22,990 --> 00:21:24,620
But yeah guys look at this.

324
00:21:24,620 --> 00:21:25,780
This is awesome.

325
00:21:25,790 --> 00:21:28,370
We are downloading our trending repots data.

326
00:21:28,460 --> 00:21:32,280
We're returning them and we get a nice dictionary.

327
00:21:32,300 --> 00:21:34,490
So let's go ahead and let's let's delete this.

328
00:21:34,490 --> 00:21:35,770
We're not supposed to call it here.

329
00:21:35,780 --> 00:21:38,170
We're going to call it in our download service later.

330
00:21:38,210 --> 00:21:41,630
But guys we are downloading all of our repository dictionary data.

331
00:21:41,630 --> 00:21:43,110
This is amazing.

332
00:21:43,160 --> 00:21:47,780
In the next video we're going to do is we're going to actually create a function that will download

333
00:21:47,870 --> 00:21:54,550
individual repo data and then return an instance of our repo model so that we can set up ourselves.

334
00:21:54,560 --> 00:21:55,690
Very very cool.

335
00:21:55,700 --> 00:21:58,300
Let's head over to that next video now and let's do it.
