-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy path10. Multithreading and Autolayout.srt
7317 lines (5840 loc) · 151 KB
/
10. Multithreading and Autolayout.srt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1
00:00:00,401 --> 00:00:04,703
[MUSIC]
2
00:00:04,772 --> 00:00:09,475
Stanford University.
>> All right, well,
3
00:00:09,543 --> 00:00:13,578
welcome to Stanford CS193P,
this is Fall of 2017,
4
00:00:13,647 --> 00:00:17,249
I think this is lecture number
10. And we have two very
5
00:00:17,317 --> 00:00:21,453
important topics today. The
first is multithreading and
6
00:00:21,522 --> 00:00:25,457
the second is autolayout size
classes. So let's talk about
7
00:00:25,525 --> 00:00:29,161
multithreading first.
Multithreading is all about,
8
00:00:29,230 --> 00:00:32,197
for the purposes of this
lecture anyway, keeping long
9
00:00:32,266 --> 00:00:36,034
running things off of the main
queue where the UI runs. And
10
00:00:36,103 --> 00:00:38,703
that's because we want the UI
to be incredibly responsive.
11
00:00:38,772 --> 00:00:41,639
When someone touches down,
we wanna immediately respond
12
00:00:41,708 --> 00:00:45,277
to their touch and
not be having our app freeze.
13
00:00:45,346 --> 00:00:48,613
It's just death to have your
app freeze, even for one
14
00:00:48,682 --> 00:00:51,416
second is an eternity for your
app to just be not responding
15
00:00:51,484 --> 00:00:54,953
at all. So multithreading
is a much bigger topic.
16
00:00:55,021 --> 00:00:56,688
You can do a lot of other
stuff with multithreading. But
17
00:00:56,756 --> 00:00:59,558
we're gonna focus on trying
to get long running things.
18
00:00:59,627 --> 00:01:03,828
Things that might block off
onto other threads. Now,
19
00:01:03,897 --> 00:01:09,167
the way that multithreading
works in iOS is using queues.
20
00:01:09,236 --> 00:01:11,836
So I'm using queue
in the sense of like
21
00:01:11,905 --> 00:01:13,504
you go to the movies and
you stand in line,
22
00:01:13,573 --> 00:01:15,240
that's a queue, right? And
23
00:01:15,308 --> 00:01:19,278
what's in the queues in iOS
multithreading is blocks of
24
00:01:19,346 --> 00:01:23,147
code, almost always closures
that you put in this queue.
25
00:01:23,216 --> 00:01:25,750
And so you got these queues,
there's multiple different
26
00:01:25,819 --> 00:01:29,188
kinds of queues. And the
system then comes along and
27
00:01:29,256 --> 00:01:32,958
it has threads. Threads
are threads of execution.
28
00:01:33,027 --> 00:01:36,027
They're essentially
opportunities to run code.
29
00:01:36,096 --> 00:01:40,131
And it can run them kind of
in parallel. I actually guess
30
00:01:40,200 --> 00:01:43,235
if you had a multiprocessor,
or a multi-core processor,
31
00:01:43,304 --> 00:01:45,971
it could actually run
things in parallel. But
32
00:01:46,039 --> 00:01:48,540
even if you had a single-core,
single thread of execution
33
00:01:48,608 --> 00:01:51,109
processor, the operating
system knows how to share
34
00:01:51,177 --> 00:01:54,346
that time up in tiny little
increments between all
35
00:01:54,414 --> 00:01:59,084
of these threads of execution.
So the OS comes along and
36
00:01:59,153 --> 00:02:01,719
takes things off the queue,
the next person in line,
37
00:02:01,788 --> 00:02:05,123
the next closure in line,
takes it off and it runs it.
38
00:02:05,192 --> 00:02:07,659
So that's how we do queues.
And there's two kinds of
39
00:02:07,727 --> 00:02:12,196
queues. We have serial queues,
which is iOS comes along,
40
00:02:12,265 --> 00:02:14,933
takes something off the queue
and then as soon as that
41
00:02:15,001 --> 00:02:17,436
thing has run to completion
then it goes back and
42
00:02:17,504 --> 00:02:20,172
gets another one off that
queue, that's called a serial
43
00:02:20,240 --> 00:02:22,707
queue. Then there's
also concurrent queues.
44
00:02:22,776 --> 00:02:25,177
Where iOS comes along and it
grabs something off the queue
45
00:02:25,245 --> 00:02:27,578
and it starts it running and
maybe it has another thread
46
00:02:27,647 --> 00:02:28,813
that it can use and
it goes and
47
00:02:28,882 --> 00:02:31,383
grabs another thing before the
other one is even finished,
48
00:02:31,451 --> 00:02:33,952
and maybe another. And so
it might have two or three or
49
00:02:34,021 --> 00:02:36,455
four or ten things running
off of the same queue all
50
00:02:36,523 --> 00:02:39,391
at the same time. That's
called a concurrent queue.
51
00:02:39,459 --> 00:02:41,893
So we're gonna talk about both
of those kinds of queues.
52
00:02:41,962 --> 00:02:43,895
Cuz we're gonna use both
of those kinds of queues
53
00:02:43,964 --> 00:02:46,998
to accomplish what we want.
And what we want is for
54
00:02:47,067 --> 00:02:48,834
the UI to be unblocked.
55
00:02:48,903 --> 00:02:53,671
And the UI runs on
a single serial queue,
56
00:02:53,740 --> 00:02:57,976
called the main queue. And not
only does the UI run on this,
57
00:02:58,045 --> 00:03:02,080
it's the only queue that can
have blocks of code put on it
58
00:03:02,148 --> 00:03:06,418
that do UI things. So we keep
all of our UI stuff here.
59
00:03:06,486 --> 00:03:08,787
That way we don't have to
worry about multithreaded UI,
60
00:03:08,856 --> 00:03:11,422
where we got two different
threads of execution both
61
00:03:11,491 --> 00:03:13,358
trying to draw into
the same space or whatever.
62
00:03:13,427 --> 00:03:15,226
We never have to worry
about that in iOS because
63
00:03:15,295 --> 00:03:19,765
all drawing, all UI activity
happens on this one queue.
64
00:03:19,833 --> 00:03:22,968
And since it's a serial queue,
it all happens on a single
65
00:03:23,036 --> 00:03:26,505
thread. So you never have to
worry about multithreaded UI
66
00:03:26,573 --> 00:03:30,509
activity going on. Now, the
way they make queue works is
67
00:03:30,577 --> 00:03:33,278
it mostly sits there waiting
for a touch event to happen.
68
00:03:33,346 --> 00:03:35,513
And when the touch event
happens, it processes it,
69
00:03:35,582 --> 00:03:38,049
figures out what code to run,
runs your code, and then goes
70
00:03:38,118 --> 00:03:40,785
back into this quiet state,
waiting for another touch.
71
00:03:40,854 --> 00:03:43,354
Well, when it's in the quiet
state, it could also pull
72
00:03:43,423 --> 00:03:46,358
something off the main queue
and run it. So you can put
73
00:03:46,426 --> 00:03:49,961
things onto the main queue,
blocks of code, and it will
74
00:03:50,029 --> 00:03:54,132
run in the UI thread, the main
queue, the main thread. So
75
00:03:54,201 --> 00:03:57,502
our goal is to get everything
else off the main queue.
76
00:03:57,571 --> 00:03:59,971
And anything else is
gonna take a long time or
77
00:04:00,040 --> 00:04:01,473
certainly is gonna
block waiting for
78
00:04:01,541 --> 00:04:02,740
the network or
something like that,
79
00:04:02,809 --> 00:04:05,943
we definitely want that off
the main queue. So where do we
80
00:04:06,012 --> 00:04:09,414
put those things? Well,
we put those in global queues.
81
00:04:09,483 --> 00:04:11,983
Now, there's actually you can
create your own queue to do
82
00:04:12,052 --> 00:04:15,319
that, but we're gonna use one
of these four global queues.
83
00:04:15,388 --> 00:04:18,122
And these are concurrent
queues that you can throw your
84
00:04:18,191 --> 00:04:21,593
code on and the system will
just come along and run them.
85
00:04:21,661 --> 00:04:24,196
And there's really no
restriction there about what
86
00:04:24,264 --> 00:04:27,131
you can put in there. It's
just, it can't be UI code. But
87
00:04:27,200 --> 00:04:29,333
you can do other stuff
all you want. So
88
00:04:29,402 --> 00:04:31,870
let's talk about those queues.
How do you get the main queue?
89
00:04:31,938 --> 00:04:33,538
How do you get a hold of it?
Well,
90
00:04:33,607 --> 00:04:36,974
there is a struct or class
called DispatchQueue and it
91
00:04:37,043 --> 00:04:40,878
has a static var called main,
that gives you the main queue.
92
00:04:40,947 --> 00:04:42,180
So now you have
the main queue,
93
00:04:42,248 --> 00:04:44,583
you're ready to go, and I'm
gonna talk to you soon about
94
00:04:44,651 --> 00:04:46,718
how you put a block of code
into the queue, right?
95
00:04:46,787 --> 00:04:50,154
Put it at the end of the line,
waiting to run.
96
00:04:50,223 --> 00:04:53,725
The global queues
are a little more expressive.
97
00:04:53,794 --> 00:04:56,027
There's not just one of them,
there's four different
98
00:04:56,096 --> 00:04:59,230
kinds here that we're going to
talk about. And the four of
99
00:04:59,299 --> 00:05:03,068
them are different based on
their quality of service.
100
00:05:03,136 --> 00:05:06,070
That's what this QOS that
you see referenced here.
101
00:05:06,139 --> 00:05:09,106
The quality of service tells
you what kind of thing,
102
00:05:09,175 --> 00:05:12,277
what kind of activity the
blocks that you're gonna put
103
00:05:12,345 --> 00:05:15,079
on there are doing. So let's
look at the four. The first
104
00:05:15,148 --> 00:05:18,950
one is userInteractive. This
is a very rare one to use.
105
00:05:19,019 --> 00:05:22,987
This means that the user is in
the middle of like dragging or
106
00:05:23,056 --> 00:05:25,757
pinching or something and
you wanna do something
107
00:05:25,826 --> 00:05:27,859
off the main queue
that has to happen so
108
00:05:27,927 --> 00:05:30,929
fast that it can get back to
the main queue in the middle
109
00:05:30,997 --> 00:05:33,698
of that drag. So we're talking
about highly interactive,
110
00:05:33,767 --> 00:05:36,233
tiny little pieces of
work that you might wanna
111
00:05:36,302 --> 00:05:37,201
throw off the main queue.
112
00:05:37,270 --> 00:05:40,972
The reason this is unusual to
use is because it's really so
113
00:05:41,041 --> 00:05:42,607
tiny and executes so quickly,
114
00:05:42,675 --> 00:05:45,644
you could probably do
it on the main queue.
115
00:05:45,712 --> 00:05:48,313
You're probably waiting for
it anyway on the main queue.
116
00:05:48,381 --> 00:05:51,249
So user interactive
much more readily used.
117
00:05:51,318 --> 00:05:55,019
The most common one to use is
the next one, userInitiated.
118
00:05:55,088 --> 00:05:57,989
So now this is something that
might take a very long time or
119
00:05:58,057 --> 00:05:59,224
it might take couple
of seconds, or
120
00:05:59,292 --> 00:06:01,325
it maybe take a few
milliseconds, you don't know.
121
00:06:01,394 --> 00:06:04,129
But the point about it is
the user has asked for
122
00:06:04,198 --> 00:06:06,598
it right now.
They touched on a button.
123
00:06:06,667 --> 00:06:08,699
They swiped somewhere and
they are asking for
124
00:06:08,768 --> 00:06:11,803
something to happen. So
it was initiated by the user.
125
00:06:11,872 --> 00:06:15,406
So they expect you to be
done as soon as possible. So
126
00:06:15,475 --> 00:06:17,575
you are running this
in the background.
127
00:06:17,644 --> 00:06:19,076
It's not happening
in the main thread,
128
00:06:19,145 --> 00:06:20,811
but you're trying to do
it as soon as possible.
129
00:06:20,880 --> 00:06:24,215
So this is a very
high priority queue.
130
00:06:24,284 --> 00:06:26,751
iOS is going to be pulling
things off that queue and
131
00:06:26,820 --> 00:06:29,387
running them in threads that
have very high priority
132
00:06:29,456 --> 00:06:31,622
because the user's asked for
it right now.
133
00:06:31,691 --> 00:06:34,626
The other two, background and
utility. So background things
134
00:06:34,694 --> 00:06:38,129
are things the user hasn't
asked for right away but
135
00:06:38,198 --> 00:06:41,232
they're kinda things that they
expect to be done fairly soon
136
00:06:41,301 --> 00:06:44,302
or when you have time
kind of a thing.
137
00:06:44,371 --> 00:06:46,237
And then utility ones
are even lower priority,
138
00:06:46,305 --> 00:06:49,807
those are things that your app
wants to do as part of its
139
00:06:49,876 --> 00:06:52,143
architecture. For example,
you have a big database and
140
00:06:52,212 --> 00:06:55,213
maybe every week or so or
every certain amount of data
141
00:06:55,282 --> 00:06:58,316
in the database, you wanna go
clean it up to remove though
142
00:06:58,384 --> 00:07:00,651
out the database. So that's
just utility operations, so
143
00:07:00,720 --> 00:07:03,154
that will run at very
low priority. So
144
00:07:03,223 --> 00:07:06,924
you pick the global key you
want based on the quality
145
00:07:06,993 --> 00:07:10,094
of service you want
that queue to receive.
146
00:07:10,163 --> 00:07:11,863
So you have a queue now.
147
00:07:11,932 --> 00:07:14,399
Either the main queue or one
of these background concurrent
148
00:07:14,468 --> 00:07:16,034
queues with a certain
quality of service.
149
00:07:16,103 --> 00:07:19,271
How do you put a block
of code onto that queue?
150
00:07:19,339 --> 00:07:22,974
Put it in line to, to get
run at some point. Well,
151
00:07:23,043 --> 00:07:27,111
you do it with one of these
two functions, async or sync.
152
00:07:27,180 --> 00:07:28,813
Each of them take
one argument.
153
00:07:28,881 --> 00:07:32,017
That argument is a block,
a closure. Takes no arguments,
154
00:07:32,085 --> 00:07:34,819
return no arguments.
It returns no values. So
155
00:07:34,888 --> 00:07:37,489
it's just as basic
a block as you can be.
156
00:07:37,557 --> 00:07:40,224
And what it does is it puts
that block on the queue that
157
00:07:40,293 --> 00:07:43,462
you're sending it to. Now, the
difference between async and
158
00:07:43,530 --> 00:07:45,496
sync is that async puts
it on the queue and
159
00:07:45,565 --> 00:07:48,432
returns immediately, and then
just goes on to the next line
160
00:07:48,501 --> 00:07:51,802
of code you have. So now it's
in the queue and someone
161
00:07:51,871 --> 00:07:53,971
eventually will go pull it
off the queue and run it but
162
00:07:54,040 --> 00:07:57,041
you return immediately. Sync,
it puts it on the queue and
163
00:07:57,110 --> 00:08:01,045
blocks. Waiting for someone
to take it off a queue and
164
00:08:01,114 --> 00:08:03,047
run it and for it to complete.
165
00:08:03,116 --> 00:08:07,385
So you would never do sync
on the main queue, right?
166
00:08:07,453 --> 00:08:09,554
Cuz we never wanna block
the main queue. But
167
00:08:09,623 --> 00:08:12,624
you might do sync on
a non-main queue, in fact,
168
00:08:12,692 --> 00:08:15,327
you might do sync waiting for
the main queue
169
00:08:15,395 --> 00:08:19,263
to finish something,
when you're on another queue.
170
00:08:19,332 --> 00:08:22,099
But mostly we're using async
here cuz we don't really care.
171
00:08:22,168 --> 00:08:23,601
async is short for
asynchronous.
172
00:08:23,669 --> 00:08:25,102
We don't really
care when it runs,
173
00:08:25,171 --> 00:08:26,738
we just want it to run
whenever it can run.
174
00:08:26,807 --> 00:08:29,340
So that's it. So
that's really all there is to
175
00:08:29,409 --> 00:08:31,642
multithreading, believe it or
not. It just leads to
176
00:08:31,711 --> 00:08:34,612
a little bit of interesting
programming, which you're
177
00:08:34,681 --> 00:08:37,482
gonna see here, and the things
you have to be careful with.
178
00:08:37,551 --> 00:08:39,984
Now I'm not gonna talk about
this, but you can create your
179
00:08:40,053 --> 00:08:42,953
own queues by just calling
DispatchQueue's initializer,
180
00:08:43,022 --> 00:08:46,257
which has this label argument.
That label just shows up
181
00:08:46,326 --> 00:08:49,461
in the debugger so you can
see which queue you're on.
182
00:08:49,530 --> 00:08:51,262
The debugger's full
support for queues,
183
00:08:51,331 --> 00:08:55,867
it'll show you what thread
everything is on. And
184
00:08:55,936 --> 00:08:59,937
you can also do a lot of other
things with multithreading,
185
00:09:00,006 --> 00:09:03,808
like protecting critical
sections in your code or
186
00:09:03,877 --> 00:09:06,644
doing synchronous dispatch or
locking between things.
187
00:09:06,713 --> 00:09:08,379
You can do all that stuff, I'm
not gonna talk about any of
188
00:09:08,447 --> 00:09:10,114
that. I'm just gonna talk
about how we're gonna use that
189
00:09:10,183 --> 00:09:12,583
main queue and those
background queues to keep
190
00:09:12,652 --> 00:09:16,754
things off the main queue.
There's a whole other API to
191
00:09:16,823 --> 00:09:19,958
this besides the DispatchQueue
API I'm gonna show you.
192
00:09:20,026 --> 00:09:23,194
OperationQueue and Operation
are the two classes involved
193
00:09:23,263 --> 00:09:26,030
there. And those would
be used if you're doing
194
00:09:26,099 --> 00:09:28,733
some huge
mathematical equation
195
00:09:28,801 --> 00:09:31,435
that has a lot of parallel
processing that you could do.
196
00:09:31,504 --> 00:09:34,205
But a lot of the parallel
pieces depend on each other.
197
00:09:34,274 --> 00:09:37,074
Because Operation allows
you to set up dependencies.
198
00:09:37,143 --> 00:09:39,510
This little block code depends
on this one running first. But
199
00:09:39,579 --> 00:09:42,413
I'm gonna start these all off
and just wait until the ones
200
00:09:42,482 --> 00:09:44,382
that depend get finished.
Do you see what I'm saying?
201
00:09:44,451 --> 00:09:45,550
All this dependency
management.
202
00:09:45,618 --> 00:09:47,284