Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Cocoa勉強会関西#53
Search
cockscomb
December 14, 2013
Programming
3
570
Cocoa勉強会関西#53
Key Value Observationを利用するUITableViewDataSourceについて話しました。雑談コーナーもあります。
cockscomb
December 14, 2013
Tweet
Share
More Decks by cockscomb
See All by cockscomb
jq at the Shortcuts
cockscomb
1
1.7k
GraphQL放談
cockscomb
4
1.9k
GraphQL Highway
cockscomb
28
8.2k
吉田を支える技術
cockscomb
0
2.1k
コーポレートサイトを静的化してAmplify Consoleにデプロイする
cockscomb
0
3.3k
ユーザインターフェイスと非同期処理
cockscomb
5
1.8k
GUIアプリケーションの構造と設計
cockscomb
10
10k
イカリング2におけるシングルページアプリケーション
cockscomb
2
7.4k
あなたの知らない UIKit の世界 — UITableView に UITextView を置きたい
cockscomb
1
7.3k
Other Decks in Programming
See All in Programming
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
500
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
110
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
4
620
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
250
Googleのテストサイズを活用したテスト環境の構築
toms74209200
0
300
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
2
190
C#/.NETのこれまでのふりかえり
tomokusaba
1
180
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
290
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
540
EventSourcingの理想と現実
wenas
6
2.2k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
cXML という電子商取引の トランザクションを支える プロトコルと向きあっている話
phigasui
3
2.3k
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
7
560
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
400
Imperfection Machines: The Place of Print at Facebook
scottboms
264
13k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Scaling GitHub
holman
458
140k
KATA
mclloyd
29
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Raft: Consensus for Rubyists
vanstee
136
6.6k
A Philosophy of Restraint
colly
203
16k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
Transcript
cockscomb
גࣜձࣾͯͳ Application Engineer Hatena Blog, Hatena Space cockscomb
Hatena Engineer Seminar #2 ͯͳʹ͓͚ΔϞμϯiOSΞϓϦ։ൃೖ
– @uzulla lυϥοάΞϯυυϩοϓࣦഊ͍ͬͯ͢͠%JT Γͭͭ$-*ͷDPDPB1PEΛࢍඒͨ͠ޙʹɺʮϚ εͰ৭ʑͰ͖Δ4#ศརɺ͋ͱ͜͜Μͱ͜Ͱ %O%͢Δͱόʔ͕ͷͼͨΓஔͰ͖ͯศརʂʯͬ ͯݴ͏ͷ͍͢͝IBUFOBUFDIz
– @kazuph lϞμϯʹॻ͔ͳ͔ͬͨ߹ͱॻ͍ͨ߹Λίʔ υͰൺֱͨͭ͠ͷͰઆ໌Λͯ͘͠ΕͯΔͷͰ ͍͢͝ḿΔIBUFOBUFDIz
͜Ε͕MVC™Ͱ͢
UITableViewDataSource
Bookmarks View BookmarkManager - (NSArray *)bookmarks TableViewController Refer Update
in TableViewController [[IBKMBookmarkManager sharedManager] reloadBookmarksWithBlock:^(NSError *error) { if (error) {
NSLog(@"Error: %@", error); } [self.tableView reloadData]; }];
Better Way [[IBKMBookmarkManager sharedManager] reloadBookmarksWithBlock:^(NSError *error) { if (error) {
NSLog(@"Error: %@", error); } }];
Key Value Observation
Observe [[IBKMBookmarkManager sharedManager] addObserver:self forKeyPath:@"bookmarks" options:NSKeyValueObservingOptionNew context:nil];
- (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]; } }
in Bookmarks Manager [[self mutableArrayValueForKey:@"bookmarks"] insertObjects:newBookmarks atIndexes:[NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, newBookmarks.count)]];
Mutable array proxy
Bookmarks View BookmarkManager - (NSArray)bookmarks TableViewController Observe Notify Update
Is it, eh?
Problems • Lightweight view controller • Copy and paste? •
Testability
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;
Observer Data Source • Useful for many occasions • DRY
• Testable • Applicable to UICollectionView
This is the MVC™
References Lighter View Controllers — objc.io Key-Value Coding and Observing
— objc.io Key-Value Observing — NSHipster
ࡶஊίʔφʔ
ʰiOS Core Data పఈೖʱ • ࠷ॳͷ͘Β͍ಡ·ͳ͍͍ͯ͘ • Core Dataʹ͍͓͓ͭͯΑͦཏత •
ຊޠͷใ͋·Γͳ͍ͷͰՁ͕ߴ͍ • ϚϧνεϨουͷ͜ͱॻ͍ͯͳ͍ • ύϑΥʔϚϯεͷ͜ͱॻ͍ͯͳ͍
iOS 7 ྑ͗͢Δ • CFAutorelease() • NSArray -firstObject • TextKit
• JavaScriptCore • Others
WebView JavaScript Bridging • - webView: shouldStartLoadWithRequest: navigationType: • Cordova
• WebViewJavascriptBridge • JSContext JSContext *ctx = [self.webView valueForKeyPath: @"documentView.webView.mainFrame.javaScriptContext"]; JSValue *three = [ctx evaluateScript:@“1+2"];
ੵۃ࠾༻த http://www.hatena.ne.jp/company/staff