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

Cocoa勉強会関西#53

58b40ae3b0db6cf0202a3802e9dd70fe?s=47 cockscomb
December 14, 2013

 Cocoa勉強会関西#53

Key Value Observationを利用するUITableViewDataSourceについて話しました。雑談コーナーもあります。

58b40ae3b0db6cf0202a3802e9dd70fe?s=128

cockscomb

December 14, 2013
Tweet

Transcript

  1. cockscomb

  2. גࣜձࣾ͸ͯͳ Application Engineer Hatena Blog, Hatena Space cockscomb

  3. Hatena Engineer Seminar #2 ͸ͯͳʹ͓͚ΔϞμϯiOSΞϓϦ։ൃೖ໳

  4. – @uzulla lυϥοάΞϯυυϩοϓ͸ࣦഊ͠΍͍ͬͯ͢%JT Γͭͭ$-*ͷDPDPB1PEΛࢍඒͨ͠௚ޙʹɺʮϚ ΢εͰ৭ʑͰ͖Δ4#ศརɺ͋ͱ͜͜Μͱ͜Ͱ %O%͢Δͱόʔ͕ͷͼͨΓ഑ஔͰ͖ͯศརʂʯͬ ͯݴ͏ͷ͍͢͝IBUFOBUFDIz

  5. – @kazuph lϞμϯʹॻ͔ͳ͔ͬͨ৔߹ͱॻ͍ͨ৔߹Λίʔ υͰൺֱͨ͠΍ͭͷͰઆ໌Λͯ͘͠ΕͯΔͷͰ ͍͢͝ḿΔIBUFOBUFDIz

  6. ͜Ε͕MVC™Ͱ͢

  7. UITableViewDataSource

  8. Bookmarks View BookmarkManager - (NSArray *)bookmarks TableViewController Refer Update

  9. in TableViewController [[IBKMBookmarkManager sharedManager] reloadBookmarksWithBlock:^(NSError *error) { if (error) {

    NSLog(@"Error: %@", error); } [self.tableView reloadData]; }];
  10. Better Way [[IBKMBookmarkManager sharedManager] reloadBookmarksWithBlock:^(NSError *error) { if (error) {

    NSLog(@"Error: %@", error); } }];
  11. Key Value Observation

  12. Observe [[IBKMBookmarkManager sharedManager] addObserver:self forKeyPath:@"bookmarks" options:NSKeyValueObservingOptionNew context:nil];

  13. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if

    (object == [IBKMBookmarkManager sharedManager] && [keyPath isEqualToString:@"bookmarks"]) { NSIndexSet *indexSet = change[NSKeyValueChangeIndexesKey]; NSKeyValueChange changeKind = (NSKeyValueChange)[change[NSKeyValueChangeKindKey] integerValue]; ! NSMutableArray *indexPaths = [NSMutableArray array]; [indexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop) { [indexPaths addObject:[NSIndexPath indexPathForRow:index inSection:0]]; }]; ! [self.tableView beginUpdates]; if (changeKind == NSKeyValueChangeInsertion) { [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } else if (changeKind == NSKeyValueChangeRemoval) { [self.tableView deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } else if (changeKind == NSKeyValueChangeReplacement) { [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } [self.tableView endUpdates]; } }
  14. in Bookmarks Manager [[self mutableArrayValueForKey:@"bookmarks"] insertObjects:newBookmarks atIndexes:[NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, newBookmarks.count)]];

    Mutable array proxy
  15. Bookmarks View BookmarkManager - (NSArray)bookmarks TableViewController Observe Notify Update

  16. Is it, eh?

  17. Problems • Lightweight view controller • Copy and paste? •

    Testability
  18. The Best Way self.observerDataSource = [[ObserverTableViewDataSource alloc] initWithObserved:[IBKMBookmarkManager sharedManager] keyPath:NSStringFromSelector(@selector(bookmarks))

    tableView:self.tableView superDataSource:self]; ! self.observerDataSource.configureCell = ^UITableViewCell *(UITableView *tableView, NSIndexPath *indexPath, id object) { static NSString *const CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; ! ... ! return cell; }; ! self.tableView.dataSource = self.observerDataSource;
  19. Observer Data Source • Useful for many occasions • DRY

    • Testable • Applicable to UICollectionView
  20. This is the MVC™

  21. References Lighter View Controllers — objc.io Key-Value Coding and Observing

    — objc.io Key-Value Observing — NSHipster
  22. ࡶஊίʔφʔ

  23. ʰiOS Core Data పఈೖ໳ʱ • ࠷ॳͷ൒෼͘Β͍͸ಡ·ͳͯ͘΋͍͍ • Core Dataʹ͍͓͓ͭͯΑͦ໢ཏత •

    ೔ຊޠͷ৘ใ͋·Γͳ͍ͷͰՁ஋͕ߴ͍ • ϚϧνεϨουͷ͜ͱ͸ॻ͍ͯͳ͍ • ύϑΥʔϚϯεͷ͜ͱ΋ॻ͍ͯͳ͍
  24. iOS 7 ྑ͗͢Δ • CFAutorelease() • NSArray -firstObject • TextKit

    • JavaScriptCore • Others
  25. WebView JavaScript Bridging • - webView:
 shouldStartLoadWithRequest:
 navigationType: • Cordova

    • WebViewJavascriptBridge • JSContext JSContext *ctx = [self.webView valueForKeyPath: @"documentView.webView.mainFrame.javaScriptContext"]; JSValue *three = [ctx evaluateScript:@“1+2"];
  26. ੵۃ࠾༻த http://www.hatena.ne.jp/company/staff