Slide 1

Slide 1 text

Frameworks Yasuhiro Inami Ex-iOS Engineer #potatotips 2014-12-17 Session 203 © 2014 @inamiy, All rights reserved. Redistribution or public display not permitted without written permission from @inamiy. Introducing ReactKit

Slide 2

Slide 2 text

Who are you? Yasuhiro Inami (@inamiy) LINE corp / NAVER Matome Ex-iOS / Java Server Engineer • Objective-C: 4 years, 26 repos • Java: 10 months, 0 repo • Swift: 6 months, 8 repos

Slide 3

Slide 3 text

https://github.com/ReactKit

Slide 4

Slide 4 text

Overview What is ReactKit Demo How it works • SwiftState • SwiftTask When to use

Slide 5

Slide 5 text

What is ReactKit Swift Reactive Programming

Slide 6

Slide 6 text

let signal = KVO.signal(obj1, "value") ! // REACT (obj2, "value") <~ signal ! XCTAssertEqual(obj1.value, "initial") XCTAssertEqual(obj2.value, "initial") ! obj1.value = "hoge" ! XCTAssertEqual(obj1.value, "hoge") XCTAssertEqual(obj2.value, "hoge") // reactive! Key-Value Observing/Coding

Slide 7

Slide 7 text

self.signal = Notification.signal("MyNotification", obj1).map { notification -> NSString? in ! return notification!.userInfo!["hoge"] as? NSString ! } ! // REACT (obj2, "value") <~ self.signal! NSNotification

Slide 8

Slide 8 text

// UITextField self.textFieldSignal = self.textField?.textChangedSignal() ! (self.label, "text") <~ self.textFieldSignal! ! ! // UIButton self.buttonSignal = self.button?.buttonSignal { _ in return "OK" } ! (self.label, "text") <~ self.buttonSignal! Target-Action

Slide 9

Slide 9 text

Demo ReactKitCatalog

Slide 10

Slide 10 text

How it works The underlying frameworks

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

State Machine

Slide 13

Slide 13 text

SwiftState Elegant state machine for Swift State & Event (Enum/String) based transition manager • Guard conditions • Wildcard state (AnyState) • Success/Error/Entry/Exit handlers with UInt8 order • Removable routes & handlers Easy Swift Syntax • Transition: .State0 => .State1 • Transition chaining: .State0 => .State1 => .State2 • Try transition: machine <- .State1 • Try transition + messaging: 
 machine <- (.State1, "GoGoGo") • Try event: machine <-! .Event1

Slide 14

Slide 14 text

SwiftState Elegant state machine for Swift [New!] Hierarchical State Machine

Slide 15

Slide 15 text

State Machine ↓ Promise

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

SwiftTask Promise + progress + pause + cancel + retry Future fulfilled/rejected value-pipelining with • progress • pause/resume • cancel • [New!] try (retryable) Pure Swift (for single task) Very thin wrapper around callback-hell objects Focused on pipelining only • no concurrency programming implemented • use whatever you want inside task-definition • e.g. GCD, NSThread, NSOperation, POSIX threads

Slide 19

Slide 19 text

Demo SwiftState & SwiftTask

Slide 20

Slide 20 text

State Machine ↓ Promise ↓ Reactive

Slide 21

Slide 21 text

ReactKit Core code public class Signal: Task { public let name: String public init(name: String = "Default", initClosure: Task.InitClosure) { self.name = name super.init(weakified: true, initClosure: initClosure) } }

Slide 22

Slide 22 text

That’s it!

Slide 23

Slide 23 text

ReactKit Signal Operations // Signal Operations public extension Signal { public func filter(filterClosure: T -> Bool) -> Signal public func map(transform: T -> U) -> Signal public func take(maxCount: Int) -> Signal public func take(until signal: Signal) -> Signal }

Slide 24

Slide 24 text

ReactKit Signal Operations Instance Methods (ver 0.3.0) • filter(f: T -> Bool) • map(f: T -> U) • map(f: T -> Signal) (a.k.a Rx.flatMap) • map(accumulate:accumulateClosure:) (a.k.a Rx.scan) • take(count) • take(until: Signal) • skip(count) • skip(until: Signal) • buffer(count) • buffer(trigger: Signal) • throttle(timeInterval) • debounce(timeInterval) Class Methods • any(signals)

Slide 25

Slide 25 text

When to use … Now!

Slide 26

Slide 26 text

When to use … Now! Use cases • Networking • User interface binding • …and a lot Can replace if your code uses • ReactiveCocoa • PromiseKit • Bolts-iOS • FBKVOController • …and a lot

Slide 27

Slide 27 text

When to use … Now! [New!] Carthage 0.3 compatible • echo 'github "ReactKit/ReactKit" ~> 0.3.0' >> Cartfile • carthage update (--use-submodules) • Drag & drop all frameworks to embed • Enjoy ~> iOS8 Contributors wanted! • Send me GitHub-issue or pull-request • or, please star

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

More Information SwiftͰ༗ݶΦʔτϚτϯʢεςʔτϚγϯʣΛ࡞Δ - Qiita http://qiita.com/inamiy/items/cd218144c90926f9a134 ! Swiftʴ༗ݶΦʔτϚτϯͰPromiseΛ֦ு͢Δ - Qiita http://qiita.com/inamiy/items/d3579b55a3ecc28dde63 ! SwiftTaskʢPromise֦ுʣΛ࢖͏ - Qiita http://qiita.com/inamiy/items/0756339aee35849384c3 ! SwiftTask(Promise֦ு)ͰϦΞΫςΟϒϓϩάϥϛϯά - Qiita http://qiita.com/inamiy/items/d68cdb528b482ee0d8c1

Slide 30

Slide 30 text

More Information Yasuhiro Inami Ex-iOS Engineer inamiy@gmail.com