@pepibumur iOS Developer at SoundCloud GitHub: pepibumur Twitter: pepibumur
@saky iOS Developer at Letgo GitHub: isaacroldan Twitter: saky
GitDo.io our spare time project
- Programming Paradigms
- Reactive Libraries
- Reactive Motivation
- Reactive Thinking
- Reactive Caveats
- Conclusion
Data-Driven, Declarative, Dynamic, End-User, Event-Driven, Expression-Oriented, Feature-Oriented, Function-level, Generic, Imperative, Inductive, Language Oriented, Metaprogramming, Non-Structured, Nondeterministic, Parallel computing, Point-free Style, Structured, Value-Level, Probabilistic
func userDidSearch(term: String) {
let apiReults = api.search(term: term).execute()
self.items = self.adaptResults(apiResults)
self.tableView.reloadData()
}
let predicate = NSPredicate(format: "name == %@", "Pedro")
let regex = NSRegularExpression(pattern: ".+", options: 0)
####### The introduction to Reactive Programming that you've been missing
self.fetchAccountCommandFactory.githubAccount()
.map { $0.hasFeature(.ImagePicker) }
.observeOn(MainScheduler.instance)
.subscribeNext { [weak self] hasFeature in
if hasFeature {
self?.view?.showImagePicker()
} else {
self?.openSubscriptionView()
}
}
.addDisposableTo(self.disposeBag)
- _More and more..._ [PromiseKit](https://github.com/mxcl/PromiseKit), [Bolts](https://github.com/BoltsFramework), [ReactiveKit](https://github.com/ReactiveKit/ReactiveKit)...
var userName: String {
didSetΒ {
// React to changes in variables
}
}
var userName: String {
didSetΒ {
// React to changes in variables
view.updateTitle(userName)
}
}
- immutability
- Side states safe (clear source of truth)
- Binding
- Encapsulated Observable actions
- Composable
- Ease operations
- Threading (observer & execute)
- immutability
- Side states safe (clear source of truth)
- Binding
- Encapsulated Observable actions
- Composable
- Ease operations
- Threading (observer & execute)
- immutability
- Side states safe (clear source of truth)
- Binding
- Encapsulated Observable actions
- Composable
- Ease operations
- Threading (observer & execute)
- immutability
- Side states safe (clear source of truth)
- Binding
- Encapsulated Observable actions
- Composable
- Ease operations
- Threading (observer & execute)
RxSwift
Observable<String>.create { (observer) -> Disposable in
observer.onNext("next value")
observer.onCompleted()
return NopDisposable.instance // For disposing the action
}
ReactiveCocoa
SignalProducer<String>.create { (observer, disposable) in
observer.sendNext("next value")
observer.sendComplete()
}
let button = UIButton()
button.rx_controlEvent(.TouchUpInside)
.subscribeNext { _ in
print("The button was tapped")
}
NSNotificationCenter.defaultCenter()
.rx_notification("my_notification", object: nil)
.subscribeNext { notification
// We got a notification
}
self.tableView.rx_delegate // DelegateProxy
.observe(#selector(UITableViewDelegate.tableView(_:didSelectRowAtIndexPath:)))
.subscribeNext { (parameters) in
// User did select cell at index path
}
let tracksFetcher = api.fetchTracks // Background
.asObservable()
let tracksFetcher = api.fetchTracks // Background
.asObservable()
.retry(3)
.catchErrorJustReturn([])
let tracksFetcher = api.fetchTracks // Background
.asObservable()
.retry(3)
.catchErrorJustReturn([])
.map(TrackEntity.mapper().map)
let tracksFetcher = api.fetchTracks // Background
.asObservable()
.retry(3)
.catchErrorJustReturn([])
.map(TrackEntity.mapper().map)
.filter { $0.name.contains(query) }
let tracksFetcher = api.fetchTracks // Background
.asObservable()
.retry(3)
.catchErrorJustReturn([])
.map(TrackEntity.mapper().map)
.filter { $0.name.contains(query) }
.flatMap { self.rx_trackImage(track: $0) }
let tracksFetcher = api.fetchTracks // Background
.asObservable()
.retry(3)
.catchErrorJustReturn([])
.map(TrackEntity.mapper().map)
.filter { $0.name.contains(query) }
.flatMap { self.rx_trackImage(track: $0) }
.observeOn(MainScheduler.instance) // Main thread
func tracksFetcher(query: String) -> Observable<[TrackEntity]>
searchTextField
.rx_text.throttle(0.5, scheduler: MainScheduler.instance)
.flatmap(tracksFetcher)
.subscribeNext { tracks in
// Yai! Tracks searched
}
Combining / Skipping Values / Deferring / Concatenation / Deferring / Take some values / Zipping
Ease plugging observables
observable
subscribe { event
case .Next(let value):
print(value)
case .Completed:
print("completed")
case .Error(let error):
print("Error: \(error)")
}
observable // Observable<String>
.bindTo(field.rx_text)
let text: Variable<String> = Variable("")
text.asObservable()
.subscribeNext { newText
print("The text did change. New text: \(newText)")
}
self.createComment(issueId: issueId, body: body).observable()
.observeOn(MainScheduler.instance)
.doOn(onError: { [weak self] error in
// show Error
})
.subscribeCompleted { [weak self] in
// show Success
}
.addDisposableTo(self.disposeBag)
// RXSWIFT: operador para debuggear -> muestra traza
class IssuePresenter {
let commandFactory = CommandFactory()
func fetch() {
commandFactory.fetchCommand.observable()
.bindTo { self.something }
.addDisposableTo(disposeBag)
}
}
class IssuePresenter {
let commandFactory = CommandFactory()
func fetch() {
commandFactory.fetchCommand.observable()
.bindTo { self.something }
.addDisposableTo(disposeBag)
}
}
presenter -> commandFactory -> fetchCommand -> presenter
class IssuePresenter {
let commandFactory = CommandFactory()
func fetch() {
commandFactory.fetchCommand.observable()
.bindTo { [weak self] in self?.something }
.addDisposableTo(disposeBag)
}
}
presenter -> commandFactory -> fetchCommand -> presenter
// Disposable bag
let todo: String = "Add code example"
don't remember what is this about xD
rxmarbles.cmo http://community.rxswift.org github.com/rxswift/rxswift github.com/reactivecocoa/reactivecocoa