Slide 1

Slide 1 text

Combine ͱ SwiftUI USAMI Kosuke Fenrir Inc.

Slide 2

Slide 2 text

Combine • WWDC19 Ͱൃද͞Εͨ Reactive ϑϨʔϜϫʔΫ • υΩϡϝϯτʹ͸ "Reactive" ͱ͸ॻ͍ͯͳ͍͕ • RxSwift ΍ ReactiveSwift ʹͱ͔ͬͯΘΔଘࡏ • ͨͩ͠ iOS 13 Ҏ߱Ͱ͔͠ಈ࡞͠ͳ͍

Slide 3

Slide 3 text

Combine ͷ༻ޠ • ֓೦͸ Rx (RxSwift) ʹ͍ۙɺ༻ޠ͸ҟͳΔ Combine RxSwift Publisher Observable Subscriber Observer CurrentValueSubject BehaviorSubject PassthroughSubject PublishSubject

Slide 4

Slide 4 text

Publisher protocol protocol Publisher { associatedtype Output associatedtype Failure: Error } • Τϥʔͷܕ͕໌ࣔతͳͷ͸ ReactiveSwift ͱಉ༷ͷར఺ • ྫ͑͹ Failure Λ Never ʹ͢Δ͜ͱͰɺΤϥʔग़ྗ͠ͳ͍ Publisher ͕ఆٛͰ͖ΔʢRxSwift ͸͜ͷ͋ͨΓ͕ऑ͍ʣ

Slide 5

Slide 5 text

Subscriber protocol protocol Subscriber { associatedtype Input associatedtype Failure: Error } protocol Publisher { func subscribe(_ subscriber: S) where Output == S.Input, Failure == S.Failure } • subscribe ʹ੩తܕνΣοΫ͕͋Δ

Slide 6

Slide 6 text

Just / Sequence / ... • enum Publishers ʹఆٛࡁΈͷ Publisher ͕͋Δ • Publishers.Empty • Publishers.Fail • Publishers.Just • Publishers.Sequence • ͳͲ

Slide 7

Slide 7 text

Future • Publishers.Future • ͻͱ͚ͭͩ஋Λฦͯ͠ऴྃ͢Δ • Promise ͷΑ͏ͳ࢖͍ํ͕૝ఆ͞Ε͍ͯΔ

Slide 8

Slide 8 text

Operators • Publisher ͕֤छͷ Operator Λ࣋ͭ • map / flatMap / filter / ... • combineLatest / merge / zip / ... • catch / retry / ... • debounce / delay / ..

Slide 9

Slide 9 text

tryMap • tryMap / tryFilter / ... • ΤϥʔΛ throw ͨ͠৔߹ʹ Publisher ͷΤϥʔग़ྗʹ ͯ͘͠ΕΔ .tryMap { data in let decoder = JSONDecoder() try decoder.decode(Hoge.self, from: data) }

Slide 10

Slide 10 text

decode • decode ͱ͍͏ɹOperator ͕͋Δ • ࣮͸͖ͬ͞ͷίʔυ͸͜͏ॻ͚Δ .decode(Hoge.self, JSONDecoder())

Slide 11

Slide 11 text

UI ͱͷ࿈ܞ • Combine ͱ SwiftUI Λ૊Έ߹Θͤͯ࢖͏ • UIKit ͱ૊Έ߹ΘͤΔ͜ͱ΋ෆՄೳͰ͸ͳ͍͕ɾɾɾ

Slide 12

Slide 12 text

BindableObject protocol protocol BindableObject { associatedtype PublisherType : Publisher where PublisherType.Failure == Never var didChange: PublisherType { get } }

Slide 13

Slide 13 text

BindableObject Λ࡞Δ class Hoge : BindableObject { let didChange = PassthroughSubject() var value: String { didSet { didChange.send() } } }

Slide 14

Slide 14 text

SwiftUI ͱͷ bind struct HogeView : View { @ObjectBinding var model: Hoge var body: some View { Text($model.value) } } HogeView(model: hogeInstance)

Slide 15

Slide 15 text

@ObjectBinding • Swift ͷ Property Delegate ػೳʢProperty Wrapper ͱ΋ݺ͹Ε͍ͯΔʣ • SwiftUI Ͱ͸ଞʹ @Binding ΍ @State ͕͋Δ • BindableObject Λ౉͢ͱɺࣗಈతʹ didChange Λ؂ࢹ͠ ͯ஋Λߋ৽ͯ͘͠ΕΔ

Slide 16

Slide 16 text

Combine + SwiftUI ͷະདྷ • MVC ͔Βͷ୤٫ • ViewController ΍ Storyboard ͔Βͷ୤٫ • Flux ͱ૬ੑ͕ྑ͍ʁ • Action : Publisher • State ͔Β View ΁ͷ൓ө : ObjectBinding