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

Advanced Key-Value Observing

Advanced Key-Value Observing

Avatar for bratislavaios

bratislavaios

August 16, 2012
Tweet

More Decks by bratislavaios

Other Decks in Programming

Transcript

  1. void (^Blocks)(BOOL Advanced); + Anonymné funkcie, resp. kus kódu uložený

    do premennej BOOL (^shouldContinue)(BOOL success); shouldContinue = ^BOOL (BOOL success) { return success; }; shouldContinue(YES); + Kopírujú do seba premenné + Dlhodobé uloženie kopírovaním + Väčšina callbackov od iOS 4.0
  2. Bloky namiesto podtriedy + Bloky v properties spúšťané metódami @property

    (nonatomic, copy) void (^updateBlock)(); - (void)update { if (self.updateBlock) self.updateBlock(); } + Kód v inej triede, napríklad v kontroléri + Kód v kontexte, kde sa používa + Menej tried
  3. - (void)viewDidLoad { [super viewDidLoad]; self.cells = [[NSMutableArray alloc] init];

    BAiOSCell *strongFirstCell = [[BAiOSCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil]; __weak BAiOSCell *firstCell = strongFirstCell; firstCell.textLabel.text = @"Tap to update"; firstCell.updateBlock = ^{ // Set the content firstCell.detailTextLabel.text = [NSString stringWithFormat:@"%li", random()]; }; [firstCell update]; firstCell.selectionBlock = ^{ // Do something [firstCell update]; }; [self.cells addObject:firstCell]; }
  4. - (void)viewDidLoad { [super viewDidLoad]; self.cells = [[NSMutableArray alloc] init];

    BAiOSCell *strongFirstCell = [[BAiOSCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil]; __weak BAiOSCell *firstCell = strongFirstCell; firstCell.textLabel.text = @"Tap to update"; firstCell.updateBlock = ^{ // Set the content firstCell.detailTextLabel.text = [NSString stringWithFormat:@"%li", random()]; }; [firstCell update]; firstCell.selectionBlock = ^{ // Do something [firstCell update]; }; [self.cells addObject:firstCell]; }
  5. - (void)viewDidLoad { [super viewDidLoad]; self.cells = [[NSMutableArray alloc] init];

    BAiOSCell *strongFirstCell = [[BAiOSCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil]; __weak BAiOSCell *firstCell = strongFirstCell; firstCell.textLabel.text = @"Tap to update"; firstCell.updateBlock = ^{ // Set the content firstCell.detailTextLabel.text = [NSString stringWithFormat:@"%li", random()]; }; [firstCell update]; firstCell.selectionBlock = ^{ // Do something [firstCell update]; }; [self.cells addObject:firstCell]; }
  6. - (void)viewDidLoad { [super viewDidLoad]; self.cells = [[NSMutableArray alloc] init];

    BAiOSCell *strongFirstCell = [[BAiOSCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil]; __weak BAiOSCell *firstCell = strongFirstCell; firstCell.textLabel.text = @"Tap to update"; firstCell.updateBlock = ^{ // Set the content firstCell.detailTextLabel.text = [NSString stringWithFormat:@"%li", random()]; }; [firstCell update]; firstCell.selectionBlock = ^{ // Do something [firstCell update]; }; [self.cells addObject:firstCell]; }
  7. - (void)viewDidLoad { [super viewDidLoad]; self.cells = [[NSMutableArray alloc] init];

    BAiOSCell *strongFirstCell = [[BAiOSCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:nil]; __weak BAiOSCell *firstCell = strongFirstCell; firstCell.textLabel.text = @"Tap to update"; firstCell.updateBlock = ^{ // Set the content firstCell.detailTextLabel.text = [NSString stringWithFormat:@"%li", random()]; }; [firstCell update]; firstCell.selectionBlock = ^{ // Do something [firstCell update]; }; [self.cells addObject:firstCell]; }
  8. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.cells.count; } - (UITableViewCell

    *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.cells objectAtIndex:indexPath.row]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Cell knows what to do [[self.cells objectAtIndex:indexPath.row] didSelect]; }
  9. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.cells.count; } - (UITableViewCell

    *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.cells objectAtIndex:indexPath.row]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Cell knows what to do [[self.cells objectAtIndex:indexPath.row] didSelect]; }
  10. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.cells.count; } - (UITableViewCell

    *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.cells objectAtIndex:indexPath.row]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Cell knows what to do [[self.cells objectAtIndex:indexPath.row] didSelect]; }
  11. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.cells.count; } - (UITableViewCell

    *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.cells objectAtIndex:indexPath.row]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView deselectRowAtIndexPath:indexPath animated:YES]; // Cell knows what to do [[self.cells objectAtIndex:indexPath.row] didSelect]; }
  12. GCD

  13. Grand Central Dispatch + Nízkoúrovňová C knižnica + Nahradzuje thready

    za queues + Vytvorené pre viacjadrové počítače + Pracuje s blokmi dispatch_async dispatch_sync dispatch_after dispatch_apply dispatch_once
  14. dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(backgroundQueue, ^{ // Do heavy

    task in background NSString *content = [self parse]; dispatch_async(dispatch_get_main_queue(), ^{ // Update UI on main queue/thread self.contentLabel.text = content; }); });
  15. dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(backgroundQueue, ^{ // Do heavy

    task in background NSString *content = [self parse]; dispatch_async(dispatch_get_main_queue(), ^{ // Update UI on main queue/thread self.contentLabel.text = content; }); });
  16. dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(backgroundQueue, ^{ // Do heavy

    task in background NSString *content = [self parse]; dispatch_async(dispatch_get_main_queue(), ^{ // Update UI on main queue/thread self.contentLabel.text = content; }); });
  17. dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(backgroundQueue, ^{ // Do heavy

    task in background NSString *content = [self parse]; dispatch_async(dispatch_get_main_queue(), ^{ // Update UI on main queue/thread self.contentLabel.text = content; }); });
  18. @interface NSObject (GCD) - (void)performOnMainThread:(void(^)(void))block wait:(BOOL)wait; - (void)performAsynchronous:(void(^)(void))block; - (void)performAfter:(NSTimeInterval)seconds

    block:(void(^)(void))block; @end [self performAfter:0.25 block:^{ // Something }]; [self performAsynchronous:^{ // Something }];
  19. [self observe:@"video.title" withBlock: ^(NSString *oldVideoTitle, NSString *newVideoTitle) { // Update

    UI weakSelf.titleLabel.text = newVideoTitle; }]; Available on GitHub
  20. firstCell.selectionBlock = ^{ [ }; UIViewController self NSArray cells UITableViewCell

    firstCell void (^)(void) selectionBlock strong strong copy __strong self doSomething];
  21. firstCell.selectionBlock = ^{ [ }; doSomething]; __weak id weakSelf =

    self; weakSelf __weak UIViewController self NSArray cells UITableViewCell firstCell void (^)(void) selectionBlock strong strong copy