Blocks. â¢iOS's Core Frameworks like UIKit, Foundation, etc. â¢UIView animations. â¢UIKit multitasking APIs. â¢Optio
Blocks / GCD & Multitasking Chuck Pisula
Topics ! "#$%&' ! ()*+,-./+0)*#-12'3*0%4-5(.16 ! 278-9:#020*'&2+;
Blocks
Blocks
What is a bl ock?
! .$,/--8/?:/+%/-$@-'0*0/A/+0' ! .$+0/=0->-BC2#20D-0$-:'/-E*)2*C#/'-@)$A-$:0/)-'%$3/ ! B+-B33)$=2A*02$+-$@-!"#$%&'$ ! BE*2#*C2#20D
▫ .F-7CG/%02E/H.F-.@*%0-7CG/%02E/H.-$CG/%0'-04/A'/#E/'a ▫ f4/D-)/'3$+,-0$--retainF--releaseF--autoreleaseF-*+,--copy ▫ g4Dc
Beware: retain cycles @interface MyObject : NSObject { void (^block_)(void); NSInteger counter_; }
MyObject
@end // Inside MyObject.m block_ = [^{ // ivar used, self gets retained... counter_ += 1; } retain]; - (void)dealloc { [block_ release]; }
block_ MyObject’s counter_ ivar is used
Blocks
Exampl e U sag e - A Ga me
! TA*;2+/-R/-*)/-A*&2+;-*-;*A/ ! B+,-+//,-0$-%)/*0/-*+-BWT-0$-*##$R-%#2/+0'-0$-)/;2'0/)-@$)-'%$)/-:3,*0/' ! "#$%&'-*)/-;)/*0-@$)-042' // GameController - (void)addScoreUpdateHandler:(void (^)(GameController *ctl, double score))handler;
Blocks
Exampl e U sag e - A Ga me
! .#2/+0-)/;2'0/)-*-C#$%&-0$-C/-/=/%:0/,-R4/+/E/)-04/-'%$)/-%4*+;/' double scoreScaleFactor = ...; UITextField *textField = ...;
[gameCtl addScoreUpdateHandler:^(GameController *ctl, double score) { score = score * scoreScaleFactor; [textField setText:[NSString stringWithFormat: @”Score %d”, score]]; }];
! f4/-C#$%&-2'-%*##/,-*0-*-#*0/)-,*0/-R4/+/E/)-04/-'%$)/-%4*+;/' ! g4*0-R$:#,-4*33/+-2@-0/=0Q2/#,-R*'-+$0-)/0*2+/,c
Blocks
Exampl e U sag e - A Ga me
! "#$%&'-%*+-C/-0)/*0/,-*'-7CG/%02E/H.-$CG/%0' ! (*A/.$+0)$##/)-%*+-'0$)/,-04/A-2+-*+-`89:0*C#/B))*Da @interface GameController : NSObject { NSMutableArray *scoreUpdateHandlers_; } - (void)addScoreUpdateHandler:(void (^)(GameController *ctl, double score))handler; @end
- (void)addScoreUpdateHandler:(void (^)(GameController *ctl, double score))handler { [scoreUpdateHandlers_ addObject:handler]; }
Blocks
Exampl e U sag e - A Ga me
! "#$%&'-*)/-)/0*2+/,-CD-04/-(*A/.$+0)$##/)P'-*))*D ! "#$%&'-)/0*2+-)/@/)/+%/'-0$-*+D-$CG/%0'-04/D-)/@/)/+%/, ! 8$JJJJR/-%*+-'*@/#D-:'/-04/'/-C#$%&'-*0-*+D-02A/a - (void)notifyScoreUpdateHandlers { double score = [self score]; for (void (^block)(GameController *ctl, double score) in scoreUpdateHandlers_) { block(self, score); } }
Blocks
Exampl e –! Vi e w Anim at ions - (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)c { if ([animationID isEqual:@"boxAnimation"] && [finished boolValue]) { [box_ removeFromSuperview]; box_ = nil; } } - (void)beginBoxAnimation { [box_ setFrame: [self startBoxFrame]]; [UIView [UIView [UIView [UIView [UIView
beginAnimations:@"boxAnimation" context:nil]; setAnimationDelay:1.0f]; setAnimationDuration:2.0f]; setAnimationDelegate:self]; setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[box_ setFrame: [self endBoxFrame]]; [UIView commitAnimations]; }
! Z*'2/)-0$-)/*,-*+,-A*2+0*2+-2@-*+2A*02$+-%$,/-R*'-2+-$+/-3#*%/-2+#2+/ ! g$:#,-'2A3#2@D-%$,2+;-2@-R/-%$:#,-)/A$E/-C$2#/)3#*0/-*+2A*02$+-%$,/
Blocks
Exampl e –! Vi e w Anim at ions
! g204-"#$%&'->-%#/*+/)F-A$)/-/=3)/''2E/F-*+,-/*'2/)-0$-A*2+0*2+ - (void)runBoxAnimation { [box_ setFrame: [self startBoxFrame]]; [UIView animateWithDuration:2.0f delay:1.0f options:0 animations: ^(void) { [box_ setFrame: [self endBoxFrame]]; } completion:^(BOOL finished) { if (finished) { [box_ removeFromSuperview]; box_ = nil; } }]; }
Blocks
Some Sho rth and
! T@-*-C#$%&-4*'-+$-3*)*A/0/)'F-04/-()-%*+-C/-$A200/, ! B##-D$:-+//,-2'-^{ } [UIView animateWithDuration:5.0 animations:^{ [box_ setFrame: [self endBoxFrame]]; }];
Blocks
N aming Con v en tio ns
! M-C#$%&-3/)-A/04$,-$)-@:+%02$+ ! "#$%&-3*)*A/0/)-'4$:#,-C/-04/-#*'0-3*)*A/0/) ! fD32%*#-+*A/'
▫ C#$%& ▫ 4*+,#/) ▫ 3)/,2%*0/ ▫ [$$&-*0-`8B))*DF-`812%02$+*)D-*+,-@$##$R-04/2)-#/*,
GCD Grand Central DIspatch
Grand Central Dispatch Why GCD?
! Z+E2'2$+-*-3)$C#/A-04*0-%*+-C/-'3#20-2+0$-'/3*)*0/F-2+,/3/+,/+0-C#$%&' ! b$R-A*+D-04)/*,'-'4$:#,-D$:-%)/*0/c
▫ Zh%2/+0-,D+*A2%-04)/*,-A*+*;/A/+0-2'-4*), ▫ 9*&2+;-$302A*#-,/%2'2$+-)/?:2)/'-;#$C*#-&+$R#/,;/ ▫ 7+-,/'&0$3F-R*+0-0$-$302A*##D-:'/-A:#023#/-%$)/' ! [$+;-):++2+;-0*'&'-'4$:#,-*E$2,-C#$%&2+;-A*2+-04)/*,
▫ 84:i2+;-R$)&-C/0R//+-04)/*,'-2'-A/''D-5'3*;4/002-*+D$+/c6 ! T+-;/+/)*#F-%$+%:))/+0-3)$;)*AA2+;-2'-4*),
▫ .$+'2,/)/,-4*)A@:#-/E/+]
Grand Central Dispatch Writ e less code , get more done!
! Zh%2/+0-'%4/,:#2+;-$@-A:#023#/-2+,/3/+,/+0-%4:+&'-5("#)*$6-$@-R$)& ! BC'0)*%0'-04)/*,2+;-,/0*2#'-*R*D-@)$A-04/-3)$;)*AA/)
▫ 78-A*+*;/'-*+,-,/0/)A2+/'-$302A*#-+:AC/)-$@-04)/*,'F-/0%J ! (.1-3)$E2,/'-:;:'&36"->-0*'&'-/=/%:0/-*+,-%$A3#/0/-$+/-*0-*-02A/-2+-QTQ7-$),/)▫ ?632->-0*'&'-/=/%:0/-'/)2*##D-$+-D$:)-*33#2%*02$+P'-A*2+-5ST6-04)/*, ▫ 8:'3/+,F-O/':A/F-.*+%/#F-W)2$)202n/ ! 12'3*0%4-()$:3'->-*;;)/;*0/-C#$%&-'D+%4)$+2n*02$+-*%)$''-A:#023#/-?:/:/' ! 12'3*0%4-8$:)%/'->-A$+20$)-#$RH#/E/#-*%02E20D-5:+2=-@,F-'$%&/0'F-A*%4-3$)0'F]6 ! -8/A*34$)/'->-/h%2/+0-%$:+02+;-'/A*34$)/
GCD
T y pical Uses
! BE$2,-C#$%&2+;-04/-ST-04)/*,-:'2+;-7"#(6"5)#2)%&&'2+5=%'%'$ ! "*%&;)$:+,-04)/*,'-'%4/,:#/-ST-:3,*0/'-$+-04/-?6325=%'%' ! 8%4/,:#/-02A/-%$+':A2+;-R$)&-$o-04/-A*2+-04)/*, ! 8/)E2%2+;-04/-+/0R$)& ! >'&36"5=%'%'$-:'/,-*'-*-)/3#*%/A/+0-@$)-#$%&2+;
GCD
Accessin g Qu e ues
! f4)//-*:0$A*02%*##D-%)/*0/,-'D'0/A-%$+%:))/+0-?:/:/'
▫ [$RF-9/,2:AF-b2;4-3)2$)20D ▫ B%%/''-:'2+;--dispatch_get_global_queue(...); ! 9*2+-04)/*,-'/)2*#-?:/:/
▫ B%%/''-:'2+;-dispatch_get_main(...); ! .)/*0/-D$:)-$R+-'/)2*#-?:/:/'
▫ .)/*0/->-dispatch_queue_create(“my
queue label”, NULL);
▫ O/#/*'/->-dispatch_release(myQueue);
GCD
D ispat ch ing B lo cks
! B'D+%4)$+$:'▫ dispatch_async(q, ^{ ... });
! 8D+%4)$+$:'->-R*20'-0$-)/0:)+-:+02#-C#$%&-%$A3#/0/'-5:'/-'3*)2+;#D6
▫ -dispatch_sync(q,
^{ ... });
▫ p-,2'3*0%4q'D+%-$+0$-04/-%:))/+0-?:/:/-D$:r##-,/*,#$%&-a ! B@0/)-*-,/#*D ▫ dispatch_after(/*when*/, q, ^{ ... });
! 7+/-02A/-04)/*,>'*@/-/=/%:02$+▫ dispatch_once(&once, ^{ ... } );
GCD
Exampl e
! .$,/-,$R+#$*,'-*+-2A*;/-@)$A-04/-+/0R$)&-*+,-04/+-,2'3#*D'-20 - (void)loadImageWithURL:(NSURL *)imageURL { NS#-99>"K3.=*9.95:05/"%"L'37"M==*0+%:0'5N2
Multitasking N oti fications
! B33#/-W:'4-`$02X%*02$+-5BW`6-8/)E2%/'
▫ Z*%4-278-,/E2%/-A*2+0*2+'-$+/-3/)'2'0/+0-%$++/%02$+-0$-BW`8 ▫ B33-,/E/#$3/)-'/)E/)'-'/+,-+$02X%*02$+'-0$-,/E2%/-04)$:;4-BW`8 H J'45EH O%E/9H M*9(:"7'=4=-
! [$%*#-`$02X%*02$+'
▫ W)$E2,/'-BW`8-@/*0:)/'-R204$:0-04/-+//,-@$)-D$:)-$R+-'/)E/) ▫ Z=*A3#/-S'/->-B#*)A-.#$%&-B33
Multitasking
L ocal N otifi cat ion Exa mp l e - (void)scheduleAlarmForDate:(NSDate *)theDate { UIApplication *app = [UIApplication sharedApplication]; // Clear out the old notification before scheduling a new one. [app cancelAllLocalNotifications]; // Create a new notification UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease]; [alarm setFireDate: theDate]; [alarm setTimeZone: [NSTimeZone defaultTimeZone]]; [alarm setRepeatInterval: 0]; [alarm setSoundName: @"alarm-sound.caf"]; [alarm setAlertBody: @"Time to wake up!"]; [app scheduleLocalNotification:alarm]; }
Resources
More Info… ! "#$%&'
▫ B33#/P'-d"#$%&'-W)$;)*AA2+;-f$32%'e->-??:05$@++?)*'+,-P'89(809Q▫ "":AP'-g/C#$;H$HA*0 H KO%-0+"O*'+,-N";"??QQQ@&(0E%$@+'.?))4.?RSST?SD?RT?)%-0+P)*'+,H KO*'+,-"U0=-"V"U(0+,-N";"??QQQ@&(0E%$@+'.?))4.?RSST?SD?RT?)*'+,-P:0=-P:(0+,-
! (.1
▫ B)'-f/%4+2%*-MKJN-O/E2/R->-4003sYY02+DJ%%Y*)'HMKHNH;%, ▫ B33#/P'-(.1-f/%4-")2/@->-4003sYY02+DJ%%Y;%,H0/%4HC)2/@ ! 9:#020*'&2+;
▫ B33#/P'-dZ=/%:02+;-.$,/-2+-04/-"*%&;)$:+,e->-??:05$@++?.:P)/5EP+'E9
Questions?