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
Data Source Combinators
Search
Rob Brown
April 02, 2015
Programming
2
68
Data Source Combinators
Eliminating the boilerplate from UITableView and UICollectionView.
Rob Brown
April 02, 2015
Tweet
Share
More Decks by Rob Brown
See All by Rob Brown
High-level Concurrency
robbrown
1
57
Elixir
robbrown
1
220
MVVM
robbrown
3
250
Reactive Cocoa
robbrown
2
140
UIKit Dynamics
robbrown
0
67
iOS State Preservation and Restoration
robbrown
5
720
Anti-Patterns
robbrown
3
110
Core Animation: Beyond the Basics
robbrown
1
80
Pragmatic Blocks
robbrown
3
86
Other Decks in Programming
See All in Programming
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
170
為你自己學 Python
eddie
0
490
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
6
640
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
380
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
470
ドメインイベント増えすぎ問題
h0r15h0
2
520
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
260
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
210
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.2k
良いユニットテストを書こう
mototakatsu
11
3.4k
Оптимизируем производительность блока Казначейство
lamodatech
0
820
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
810
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
50
11k
Learning to Love Humans: Emotional Interface Design
aarron
274
40k
For a Future-Friendly Web
brad_frost
176
9.5k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Navigating Team Friction
lara
183
15k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Being A Developer After 40
akosma
89
590k
The Language of Interfaces
destraynor
155
24k
A designer walks into a library…
pauljervisheath
205
24k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Adopting Sorbet at Scale
ufuk
74
9.1k
Transcript
Data$Source$Combinators ©"Robert"Brown"March"2015"@robby_brown
Table&Views you're'doing'them'wrong ©"Robert"Brown"March"2015"@robby_brown
Overview 1. The&Hard&Way 2. Code&Architecture&Theory 3. The&Be7er&Way ©"Robert"Brown"March"2015"@robby_brown
Demo ©"Robert"Brown"March"2015"@robby_brown
The$Hard$Way ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on class CandyTableViewController : UITableViewController { override func tableView(tableView: UITableView,
numberOfRowsInSection section: Int) -> Int { return self.candies.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = self.tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell let candy = self.candies[indexPath.row] cell.textLabel!.text = candy.name cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator return cell } } From:&raywenderlich.com ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on What's'wrong'with'this? • Duplicate+boilerplate • View+controllers+handle+interac5on,+not+data+ sources • Cells+are+o8en+shared+between+table+views •
Cells+should+handle+their+own+layout ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on Why$do$we$see$this$so$o,en? • It's&"standard"&prac.ce • That's&how&I've&always&done&it • My&favorite&tutorial&does&it • Apple&does&it
©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on Why$do$we$see$this$so$o,en? • It's&really&convenient&for&tutorials&to&be&simple ©"Robert"Brown"March"2015"@robby_brown
Code%Architecture%Theory ©"Robert"Brown"March"2015"@robby_brown
Data$Sources Problems: • Massive(View(Controller • Duplicate(boilerplate ©"Robert"Brown"March"2015"@robby_brown
Data$Sources Solu%on: • Single(responsibility0principle • Combinators • Chain(of(responsibility ©"Robert"Brown"March"2015"@robby_brown
A"class"should"have"only"one" reason"to"change. —"Single)responsibility"principle ©"Robert"Brown"March"2015"@robby_brown
A"class"should"have"only"one" reason"to"change. —"Single)responsibility"principle ©"Robert"Brown"March"2015"@robby_brown
Every&class&should&have& responsibility&over&a&single& part&of&the&func6onality...,& and&that&responsibility&should& be&en6rely&encapsulated&by& the&class.1 —"Single)responsibility"principle 1"Wikipedia ©"Robert"Brown"March"2015"@robby_brown
Combinators • Technique*from*func/onal*programming • Usually*seen*with*parsers • Generally*represent*a*single,*simple*feature • Focus*on*composi/on •
Like*higher>order*func/ons*applied*to*features ©"Robert"Brown"March"2015"@robby_brown
Chain&of&Responsibility • Like&a&chain&of&delegates • A&3>&B&3>&C&3>&D&... • If&A&can't&handle&something,&then&try&B&... • Used&to&create&a&sequence&of&combinators ©"Robert"Brown"March"2015"@robby_brown
The$Be&er$Way ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on View%Controller override func viewDidLoad() { super.viewDidLoad() let objects =
[] // Get objects here tableView.dataSource = BaseDataSource(objects) { (view, indexPath, object) -> Any in return MyCell.cell(view as! UITableView, name: object as! String) } } ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on View%Controller override func viewDidLoad() { super.viewDidLoad() let objects =
[] // Get objects here let base = BaseDataSource(objects) { (view, indexPath, object) -> Any in return MyCell.cell(view as! UITableView, name: object as! String) } self.dataSource = ReorderableDataSource(base) } ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on Cell class MyCell: SmartTableViewCell { @IBOutlet weak var titleLabel:
UILabel! class func cell(tableView: UITableView, name: String) -> Self { let cell = self.cell(tableView) cell.titleLabel.text = name return cell } } ©"Robert"Brown"March"2015"@robby_brown
Cell$Crea'on Why$is$this$be*er? • Fewer&lines&of&code • Boilerplate&removed • Behavior&can&be&changed&with&the&data&source(s) • The&data&source&can&work&with&table&and&collec=on&
views • Easier&to&build&combinators ©"Robert"Brown"March"2015"@robby_brown
Dynamic(Behaviors • Array • Mul)*dimensional2array • Filtering • Reordering •
Edi)ng • Index2)tles ©"Robert"Brown"March"2015"@robby_brown
Considera*ons • The%order%of%combinators%applied%ma4ers%for% performance • Ex:%Create%index%9tles%before%filtering • Some9mes%a%combinator%needs%to%implement%more% than%one%feature%for%performance ©"Robert"Brown"March"2015"@robby_brown
Example(Combinators • BaseDataSource:#Provides#minimum# func1onality • ChainableDataSource:#Allows#data#source# sequences • FilterableDataSource:#Allows#filtering#(ex.# search#bar)
• ReorderableDataSource:#Allows#reordering • IndexableDataSource:#Shows#index#1tles ©"Robert"Brown"March"2015"@robby_brown
Demo ©"Robert"Brown"March"2015"@robby_brown
Summary 1. The&Hard&Way 2. Code&Architecture&Theory 3. The&Be7er&Way ©"Robert"Brown"March"2015"@robby_brown
Ques%ons? ©"Robert"Brown"March"2015"@robby_brown
Resources(to(Learn(More • Source(Code • Combinator • Func2onal(Programming(in(Swi7 • Func2onal(Snippets ©"Robert"Brown"March"2015"@robby_brown
Resources(to(Learn(More • Separa'on*of*Concerns • Cohesion • Orthogonality ©"Robert"Brown"March"2015"@robby_brown