1
00:00:04,860 --> 00:00:07,410
in the last video we started tidying up

2
00:00:07,410 --> 00:00:09,870
our download XML function to write it

3
00:00:09,870 --> 00:00:12,330
more how you do it in Kotlin rather than

4
00:00:12,330 --> 00:00:14,670
converted Java now that was a useful

5
00:00:14,670 --> 00:00:16,470
exercise and seeing things like the

6
00:00:16,470 --> 00:00:18,090
catch blocks being changed will help you

7
00:00:18,090 --> 00:00:20,760
to write better Kotlin code now the

8
00:00:20,760 --> 00:00:22,829
catch block change didn't save us a lot

9
00:00:22,829 --> 00:00:24,840
of code but when we replaced our while

10
00:00:24,840 --> 00:00:26,759
loop with a single line in Kotlin well

11
00:00:26,759 --> 00:00:28,290
that saved quite a bit of code

12
00:00:28,290 --> 00:00:30,960
now unless code means fewer errors so

13
00:00:30,960 --> 00:00:33,600
that's definitely an improvement so as

14
00:00:33,600 --> 00:00:34,980
promised i'm now going to replace the

15
00:00:34,980 --> 00:00:37,050
download XML function with just one line

16
00:00:37,050 --> 00:00:39,899
of Kotlin so the first thing to do is to

17
00:00:39,899 --> 00:00:41,789
deal to delete all the code we've got in

18
00:00:41,789 --> 00:00:44,609
here in there I'm going to delete

19
00:00:44,609 --> 00:00:48,239
everything out of the download XML

20
00:00:48,239 --> 00:00:56,850
function look so now at the moment we're

21
00:00:56,850 --> 00:00:58,589
getting an error because the function is

22
00:00:58,589 --> 00:01:01,679
not returning anything so we better tell

23
00:01:01,679 --> 00:01:07,070
it what to return so let's do return URL

24
00:01:07,070 --> 00:01:14,640
URL path dot retakes and that's it let's

25
00:01:14,640 --> 00:01:16,140
make sure it works before we look at

26
00:01:16,140 --> 00:01:18,570
what's actually going on you can see the

27
00:01:18,570 --> 00:01:21,900
code is just literally one line so let's

28
00:01:21,900 --> 00:01:24,120
start bring up the logcat we'll clear it

29
00:01:24,120 --> 00:01:26,310
and we'll actually run it and then if

30
00:01:26,310 --> 00:01:27,780
you need to see the emulator screen we

31
00:01:27,780 --> 00:01:30,620
just need to see the log count output

32
00:01:30,620 --> 00:01:32,700
and you can see that everything's worked

33
00:01:32,700 --> 00:01:34,140
as it's done before we're still seeing

34
00:01:34,140 --> 00:01:37,200
the XML downloaded code and the various

35
00:01:37,200 --> 00:01:39,630
uncreate doing background so on and so

36
00:01:39,630 --> 00:01:42,000
forth that's all working so basically

37
00:01:42,000 --> 00:01:43,800
the oncreate starting and finishing just

38
00:01:43,800 --> 00:01:45,660
like before then the background task

39
00:01:45,660 --> 00:01:47,670
executes with the URL that we provided

40
00:01:47,670 --> 00:01:49,890
now there's no logging for download XML

41
00:01:49,890 --> 00:01:52,140
because there's nothing really to log it

42
00:01:52,140 --> 00:01:53,490
obviously works though because we get

43
00:01:53,490 --> 00:01:55,680
we're getting the XML logged by the on

44
00:01:55,680 --> 00:01:59,010
post execute function now I will say

45
00:01:59,010 --> 00:02:00,060
that there was nothing wrong with our

46
00:02:00,060 --> 00:02:02,190
previous code and you'll see in a moment

47
00:02:02,190 --> 00:02:04,230
why you might need to use code like that

48
00:02:04,230 --> 00:02:06,480
so the steps we went through in the last

49
00:02:06,480 --> 00:02:08,940
video were important I didn't just get

50
00:02:08,940 --> 00:02:10,348
you to type in all that code just so you

51
00:02:10,348 --> 00:02:12,150
can see how impressive this is although

52
00:02:12,150 --> 00:02:14,520
I will say it is pretty impressive but

53
00:02:14,520 --> 00:02:15,810
let's actually have a look at what's

54
00:02:15,810 --> 00:02:16,890
going on

55
00:02:16,890 --> 00:02:19,560
how Commons let us replace all that code

56
00:02:19,560 --> 00:02:22,410
with just a single line now Cullen

57
00:02:22,410 --> 00:02:24,120
provides something called extension

58
00:02:24,120 --> 00:02:26,340
functions which are a great way to add

59
00:02:26,340 --> 00:02:28,470
extra functions to existing classes

60
00:02:28,470 --> 00:02:31,920
now here the Kotlin library's added read

61
00:02:31,920 --> 00:02:34,740
text as an extension function to the URL

62
00:02:34,740 --> 00:02:37,770
class no URL is a class from the

63
00:02:37,770 --> 00:02:40,170
standard Java library in the package

64
00:02:40,170 --> 00:02:43,350
java.net now you can check that by

65
00:02:43,350 --> 00:02:46,350
holding down ctrl or command on URL and

66
00:02:46,350 --> 00:02:48,720
hovering over URL as I'm doing there now

67
00:02:48,720 --> 00:02:50,489
I won't click on it because I don't yet

68
00:02:50,489 --> 00:02:52,980
have the API 26 source code but the

69
00:02:52,980 --> 00:02:54,690
tooltip tells us all we need to know and

70
00:02:54,690 --> 00:02:55,980
you can see where it's coming from

71
00:02:55,980 --> 00:03:00,060
Java declarations java.net so URL is a

72
00:03:00,060 --> 00:03:01,800
Java class that's been around for a long

73
00:03:01,800 --> 00:03:05,010
time now the colon developers wanted to

74
00:03:05,010 --> 00:03:07,050
give it a read text method but they

75
00:03:07,050 --> 00:03:08,910
can't go messing about with standard

76
00:03:08,910 --> 00:03:09,900
Java libraries

77
00:03:09,900 --> 00:03:13,019
so instead Kotlin allows extension

78
00:03:13,019 --> 00:03:15,420
functions to be attached to existing

79
00:03:15,420 --> 00:03:17,310
classes and call us so they really

80
00:03:17,310 --> 00:03:19,709
belong to that class so that's what read

81
00:03:19,709 --> 00:03:23,340
text is an extension function now we can

82
00:03:23,340 --> 00:03:25,019
have a look at it by ctrl or command

83
00:03:25,019 --> 00:03:29,220
clicking read text let's do that this

84
00:03:29,220 --> 00:03:31,739
takes us to the Kotlin readwrite module

85
00:03:31,739 --> 00:03:34,260
where Kotlin added all sorts of

86
00:03:34,260 --> 00:03:36,390
functions to standard Java classes in

87
00:03:36,390 --> 00:03:38,040
fact we've been here before because if

88
00:03:38,040 --> 00:03:40,019
you scroll a short way up we should be

89
00:03:40,019 --> 00:03:42,590
able to see the reader dot read text

90
00:03:42,590 --> 00:03:46,440
there's reader copy to and reader dot

91
00:03:46,440 --> 00:03:49,130
read text we looked at those earlier

92
00:03:49,130 --> 00:03:51,989
alright so back down to what URL don't

93
00:03:51,989 --> 00:03:54,240
read text

94
00:03:54,240 --> 00:03:56,730
and the docstring there says reads the

95
00:03:56,730 --> 00:03:59,850
entire content of this URL as a string

96
00:03:59,850 --> 00:04:02,040
and that's just what our previous code

97
00:04:02,040 --> 00:04:04,830
was doing we just did it the long way so

98
00:04:04,830 --> 00:04:07,140
read text is a function that returns a

99
00:04:07,140 --> 00:04:09,360
string so it works by calling the read

100
00:04:09,360 --> 00:04:11,670
bytes function then returning the result

101
00:04:11,670 --> 00:04:13,710
of Dennis a string by calling the two

102
00:04:13,710 --> 00:04:16,170
string function now read bytes appears

103
00:04:16,170 --> 00:04:17,180
at the end of the file

104
00:04:17,180 --> 00:04:20,220
you can see that and then read bots it

105
00:04:20,220 --> 00:04:22,320
calls open stream and uses that to read

106
00:04:22,320 --> 00:04:24,690
bytes from it now this is very similar

107
00:04:24,690 --> 00:04:26,490
to what we've seen so you probably won't

108
00:04:26,490 --> 00:04:28,500
be surprised by what we see when I

109
00:04:28,500 --> 00:04:30,330
control click on it or command or

110
00:04:30,330 --> 00:04:32,520
control click read bytes we're gonna

111
00:04:32,520 --> 00:04:34,110
come over here the oit don't read bias

112
00:04:34,110 --> 00:04:36,690
and click on read bytes over there we've

113
00:04:36,690 --> 00:04:38,670
been here as well so her read bytes uses

114
00:04:38,670 --> 00:04:41,220
that copy to function to read from the

115
00:04:41,220 --> 00:04:43,680
stream and copy to use as a while loop

116
00:04:43,680 --> 00:04:47,340
there it is just above

117
00:04:47,340 --> 00:04:49,230
as you can see on the screen there at

118
00:04:49,230 --> 00:04:51,540
the top that's just above the read bytes

119
00:04:51,540 --> 00:04:53,850
extension function now before we go back

120
00:04:53,850 --> 00:04:55,710
to our e-text in the readwrite Kotlin

121
00:04:55,710 --> 00:04:57,750
code notice are covered that it is the

122
00:04:57,750 --> 00:05:00,150
caller's responsibility to close this

123
00:05:00,150 --> 00:05:02,370
dream now this may worry you but it

124
00:05:02,370 --> 00:05:04,979
needn't so if we close our iOS dreams

125
00:05:04,979 --> 00:05:07,590
Katie now and we're back at readwrite

126
00:05:07,590 --> 00:05:10,530
Katie the use function actually takes

127
00:05:10,530 --> 00:05:13,110
care of closing down the resources as we

128
00:05:13,110 --> 00:05:16,020
saw earlier so that's handled for us now

129
00:05:16,020 --> 00:05:17,160
the last thing that I want to mention

130
00:05:17,160 --> 00:05:19,110
here is that the second line for the doc

131
00:05:19,110 --> 00:05:21,389
string for read text this method is not

132
00:05:21,389 --> 00:05:24,090
recommended on huge files now that's why

133
00:05:24,090 --> 00:05:26,460
our previous code may be useful so

134
00:05:26,460 --> 00:05:28,620
getting it all working and Kotlin wasn't

135
00:05:28,620 --> 00:05:30,540
a wasted exercise you know if you're

136
00:05:30,540 --> 00:05:32,190
dealing with a lot of data it's

137
00:05:32,190 --> 00:05:33,750
important to remember that read text

138
00:05:33,750 --> 00:05:35,550
will read the whole lot into a string in

139
00:05:35,550 --> 00:05:36,150
memory

140
00:05:36,150 --> 00:05:38,460
now what's meant by huge depends on the

141
00:05:38,460 --> 00:05:40,740
environment you might think nothing of

142
00:05:40,740 --> 00:05:42,270
reading a gigabyte on a desktop computer

143
00:05:42,270 --> 00:05:45,240
with 32 gigabytes of RAM but we're

144
00:05:45,240 --> 00:05:47,039
writing apps for Android devices though

145
00:05:47,039 --> 00:05:49,740
so even a couple of megabytes can be

146
00:05:49,740 --> 00:05:52,169
considered huge now the Downloads that

147
00:05:52,169 --> 00:05:53,700
we'll be dealing with for this feat are

148
00:05:53,700 --> 00:05:55,560
just a few kilobytes but if you

149
00:05:55,560 --> 00:05:56,970
experiment with different feeds

150
00:05:56,970 --> 00:06:00,030
definitely keep an eye on their size now

151
00:06:00,030 --> 00:06:01,560
as you browse through these Kotlin

152
00:06:01,560 --> 00:06:02,940
libraries to get a feel for what's

153
00:06:02,940 --> 00:06:04,950
provided make sure you also pay

154
00:06:04,950 --> 00:06:06,860
attention to any comments like this one

155
00:06:06,860 --> 00:06:09,930
Kotlin can make coding a lot easier but

156
00:06:09,930 --> 00:06:11,760
it's up to you to only use features when

157
00:06:11,760 --> 00:06:13,979
they are appropriate with great power

158
00:06:13,979 --> 00:06:15,680
comes great responsibility

159
00:06:15,680 --> 00:06:17,910
now before I finish I'm going to check

160
00:06:17,910 --> 00:06:20,280
the import section of main activity and

161
00:06:20,280 --> 00:06:24,570
it claims down read/write cought at the

162
00:06:24,570 --> 00:06:26,010
moment the Kotlin plugin isn't very good

163
00:06:26,010 --> 00:06:27,960
at removing unused import so we may

164
00:06:27,960 --> 00:06:30,389
still have the Java net and Java dot IO

165
00:06:30,389 --> 00:06:33,660
imports for the code we deleted now in

166
00:06:33,660 --> 00:06:35,639
my case I haven't because I've optimized

167
00:06:35,639 --> 00:06:38,160
imports to be automatically updated or

168
00:06:38,160 --> 00:06:39,539
the unused imports will be removed

169
00:06:39,539 --> 00:06:41,250
automatically but if you've got any

170
00:06:41,250 --> 00:06:43,590
unused imports here now there would be a

171
00:06:43,590 --> 00:06:46,500
good time to get rid of them and what

172
00:06:46,500 --> 00:06:47,910
you should end up is a little green dot

173
00:06:47,910 --> 00:06:49,500
will end up with is a little green tick

174
00:06:49,500 --> 00:06:52,139
in the right-hand corner we need to when

175
00:06:52,139 --> 00:06:53,639
you're when you're saying that you know

176
00:06:53,639 --> 00:06:56,070
that you're ready to continue all right

177
00:06:56,070 --> 00:06:57,780
so let's finish the video here and in

178
00:06:57,780 --> 00:06:59,130
the next one we're gonna make a start on

179
00:06:59,130 --> 00:07:01,199
passing out the information we want

180
00:07:01,199 --> 00:07:03,719
from the XML that we've downloaded see

181
00:07:03,719 --> 00:07:06,409
you in the next video

