This week i found alot of detail about Reactive Cocoa. I would to present this video if you are beginner like me haha. This video is stream on 2014 but it very useful for beginner like me. Ok don't waste time Let's get started !
- Next(T)
- A value provided by the signal.
- Error(E)
- The signal terminated because of an error. No further events will be received.
- Completed
- The signal successfully terminated, No further events will be received.
- Interrupted
- Event production on the signal has been interrupted. No futher events will be received.
- Signals model an existing stream of events in your app.
- User input
- Notification
- Location updates
- SignalProducers model the results of some work.
- Network requests
- A model view
- A watch connectivity request
- Errors are passes on to the next step in the stram.
- If any point of the stream fails, we can handel this in one place.
- Railway oriented programming.
- Less code.
- Maintainable code.
- Readable code.
- Try to get signal from UITextField by rac_textSignal, So nice! now we don't need UITextField Delegate becuase we have a useful signal.
let searchTextSignal = searchTextField.rac_textSignal()
searchTextSignal.toSignalProducer().map({ text in text as! String }).startWithNext { text in
print("search text : \(text)")
}
- Try to add filter operator is used to only include values in an event stream that satisfy a predicate.
- In this case i'm try is search string has more than 3 character, filter operator will alow and go to startWithNext
searchTextSignal.toSignalProducer()
.map({ text in text as! String })
.filter({ searchChar in searchChar.characters.count > 3 })
.startWithNext { text in
print("search text : \(text)")
}
-
Try to add methods from ReactiveCocoa adds to UIKit, rac_signalForControlEvents.
-
And add some feature with doNext operator, like when user pressed UIButton button will disable and change alpha.
let searchButtonSignal = searchButton.rac_signalForControlEvents(UIControlEvents.TouchUpInside)
searchButtonSignal.doNext( { _ in
self.searchButton.enabled = false
self.searchButton.alpha = 0.5
self.searchTextField.enabled = false
} )
.subscribeNext { _ in
print("searchButton pressed!")
}