Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Some Closure After Missing the Block Party

Some Closure After Missing the Block Party

Blocks are a powerful programming pattern introduced in iOS 4. Block-based APIs are used throughout the system now and you really need to be comfortable with the concept, especially with Swift on the horizon. We’ll take a look at the key concepts needed to work with blocks, some of the frameworks like Grand Central Dispatch that make them powerful, and then see how they’re evolving in Swift.

Curtis Herbert

June 12, 2014
Tweet

More Decks by Curtis Herbert

Other Decks in Programming

Transcript

  1. int (^myBlock)(int) = ^(int a) { return a * 15;

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

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

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

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

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

    }; ! int result = myBlock(10); Definition Implementation Return type Name Parameter types Parameters Body
  7. [UIView animateWithDuration:1 animations:^{ self.view.alpha = 0.0; }]; ! ! [myDictionary

    enumerateKeysAndObjectsUsingBlock: ^(id k, id v, BOOL *stop) { NSLog(@"%@ => %@", k, v); }];
  8. - (void)upload:(NSData *)data failure:(void (^)(NSError *))callback; ! ! ! [server

    upload:myData failure:^(NSError *error) { NSLog(@“Error!?! %@“, error); }];
  9. - (void)doLotsOfWork { for (int i = 1; i <

    100000; i++) { NSLog(@“%l”, i); } } ! ... ! NSLog(@“Before”); ! [self doLotsOfWork]; ! NSLog(@“After”);
  10. 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”);
  11. 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];
  12. 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];
  13. __block NSString *test = @“Hello!; ! void (^myBlock)() = ^(){

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

    ^(){ test = @“Hello 2!”; }; ! myBlock(); ! NSLog(test);
  15. 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"
  16. __block var test = "Hello, playground” ! var myClosure: ()

    { test = "Hello Again!" } ! myClosure ! println(test)
  17. Further reading WWDC 2010: Introducing Blocks and Grand Central Dispatch

    on iPhone The Swift Programming Language iBook http://goshdarnblocksyntax.com http://goshdarnclosuresyntax.com