際際滷

際際滷Share a Scribd company logo
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%鍖ltering
 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#鍖ltering#(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

More Related Content

Data Source Combinators