Slide 1

Slide 1 text

Some Closure After Missing the Block Party Curtis Herbert @parrots

Slide 2

Slide 2 text

What is a block?

Slide 3

Slide 3 text

^() { //do some awesome stuff };

Slide 4

Slide 4 text

void (^myBlock)() = ^() { NSLog(@“Inside!”); }; ! NSLog(@“Outside”); ! myBlock();

Slide 5

Slide 5 text

int (^myBlock)(int) = ^(int a) { return a * 15; }; ! int result = myBlock(10);

Slide 6

Slide 6 text

int (^myBlock)(int) = ^(int a) { return a * 15; }; ! int result = myBlock(10); Definition Implementation Return type Name Parameters Body Parameter types

Slide 7

Slide 7 text

int (^myBlock)(int) = ^(int a) { return a * 15; }; ! int result = myBlock(10); Definition Implementation Return type Name Parameters Body Parameter types

Slide 8

Slide 8 text

int (^myBlock)(int) = ^(int a) { return a * 15; }; ! int result = myBlock(10); Definition Implementation Return type Name Para Body Parameter types

Slide 9

Slide 9 text

int (^myBlock)(int) = ^(int a) { return a * 15; }; ! int result = myBlock(10); Definition Implementation Return type Name Parameter types Body Parameters

Slide 10

Slide 10 text

int (^myBlock)(int) = ^(int a) { return a * 15; }; ! int result = myBlock(10); Definition Implementation Return type Name Parameter types Parameters Body

Slide 11

Slide 11 text

http://f*ckingblocksyntax.com ! ! (or http://goshdarnblocksyntax.com)

Slide 12

Slide 12 text

[UIView animateWithDuration:1 animations:^{ self.view.alpha = 0.0; }]; ! ! [myDictionary enumerateKeysAndObjectsUsingBlock: ^(id k, id v, BOOL *stop) { NSLog(@"%@ => %@", k, v); }];

Slide 13

Slide 13 text

- (void)upload:(NSData *)data failure:(void (^)(NSError *))callback; ! ! ! [server upload:myData failure:^(NSError *error) { NSLog(@“Error!?! %@“, error); }];

Slide 14

Slide 14 text

Blocks + Grand Central Dispatch

Slide 15

Slide 15 text

Queue Types Concurrent Serial 1 2 3 1 2 3

Slide 16

Slide 16 text

dispatch_get_global_queue() (concurrent) • DISPATCH_QUEUE_PRIORITY_BACKGROUND • DISPATCH_QUEUE_PRIORITY_LOW • DISPATCH_QUEUE_PRIORITY_NORMAL • DISPATCH_QUEUE_PRIORITY_HIGH dispatch_get_main_queue() (serial) dispatch_queue_create();

Slide 17

Slide 17 text

- (void)doLotsOfWork { for (int i = 1; i < 100000; i++) { NSLog(@“%l”, i); } } ! ... ! NSLog(@“Before”); ! [self doLotsOfWork]; ! NSLog(@“After”);

Slide 18

Slide 18 text

Don’t block the main thread!

Slide 19

Slide 19 text

NSLog(@“Before”); ! [self performSelectorInBackground: @selector(doLotsOfWork) withObject:nil]; ! NSLog(@“After”);

Slide 20

Slide 20 text

dispatch_async

Slide 21

Slide 21 text

NSLog(@“Before”); ! dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Background Task for (int i = 1; i < 100000; i++) { NSLog(@“%l”, i); } } ); ! NSLog(@“After”);

Slide 22

Slide 22 text

NSLog(@“Before”); ! dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Background Task for (int i = 1; i < 100000; i++) { NSLog(@“%l”, i); } ! dispatch_async(dispatch_get_main_queue(),^{ //Update the UI }); } ); ! NSLog(@“After”);

Slide 23

Slide 23 text

NSLog(@“Before”); ! dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Background Task ! dispatch_async(dispatch_get_main_queue(),^{ //Update the UI }); } ); ! NSLog(@“After”);

Slide 24

Slide 24 text

dispatch_after dispatch_once, dispatch_sync, dispatch_apply

Slide 25

Slide 25 text

Blocks + NSOperationQueue

Slide 26

Slide 26 text

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; ! [myQueue addOperationWithBlock:^(void){ //Do something awesome }];

Slide 27

Slide 27 text

NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; ! NSBlockOperation *operation = [[NSBlockOperation alloc] init]; ! [operation addExecutionBlock:^{ //Do something awesome if (!operation.isCancelled) { //Keep doing awesome stuff } }]; ! [myQueue addOperation:operation]; ! [operation cancel];

Slide 28

Slide 28 text

NSBlockOperation *operation = [[NSBlockOperation alloc] init]; [operation addExecutionBlock:^{ //Do something awesome }]; ! NSBlockOperation *operation1 = [[NSBlockOperation alloc] init]; [operation1 addExecutionBlock:^{ //Do something else awesome }]; [operation1 addDependency:operation]; ! [myQueue addOperation:operation]; [myQueue addOperation:operation1];

Slide 29

Slide 29 text

–Me “Start with GCD”

Slide 30

Slide 30 text

Some gotchas Blocks capture in-scope variables (with the best intentions!)

Slide 31

Slide 31 text

__block NSString *test = @“Hello!; ! void (^myBlock)() = ^(){ test = @“Hello 2!”; }; ! myBlock(); ! NSLog(test);

Slide 32

Slide 32 text

__block __block NSString *test = @“Hello!; ! void (^myBlock)() = ^(){ test = @“Hello 2!”; }; ! myBlock(); ! NSLog(test);

Slide 33

Slide 33 text

weakself pattern self.errorHandler = ^(){ [self displayAlert:@“Oh No!”]; };

Slide 34

Slide 34 text

weakself pattern _weak typeof(self) weakSelf = self; self.errorHandler = ^(){ __strong typeof(weakSelf) strongSelf = weakSelf; ! if (strongSelf) { [strongSelf displayAlert:@“Oh No!”]; } }; pod ‘libextobjc’, #import "EXTScope.h"

Slide 35

Slide 35 text

Closures in Swift

Slide 36

Slide 36 text

{(parameterTypes) -> (returnType) in //do some awesome stuff }

Slide 37

Slide 37 text

{(name: String) -> (Bool) in return countElements(name) > 6 }

Slide 38

Slide 38 text

http://f*ckingclosuresyntax.com ! (or http://goshdarnclosuresyntax.com)

Slide 39

Slide 39 text

dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), { //Background Task ! dispatch_async(dispatch_get_main_queue(), { //Update the UI }) } )

Slide 40

Slide 40 text

__block var test = "Hello, playground” ! var myClosure: () { test = "Hello Again!" } ! myClosure ! println(test)

Slide 41

Slide 41 text

weakself: easier self.errorHandler = { self.displayAlert(“Oh No!”) }

Slide 42

Slide 42 text

weakself: easier self.errorHandler = { [unowned self] in self.displayAlert(“Oh No!”) }

Slide 43

Slide 43 text

Further reading WWDC 2010: Introducing Blocks and Grand Central Dispatch on iPhone The Swift Programming Language iBook http://goshdarnblocksyntax.com http://goshdarnclosuresyntax.com