Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cliss/b8bee47a1c4f8b7d59ae48cb2cb34d7f to your computer and use it in GitHub Desktop.
Save cliss/b8bee47a1c4f8b7d59ae48cb2cb34d7f to your computer and use it in GitHub Desktop.
// A result object that comes from the network.
// The contents are irrelevant for this example.
struct Result {
let text: String
let someOtherThing: String
}
// An object that can get data from the network
struct Fetcher {
/**
Performs a network query, and returns 0 or more results as an array.
- Parameter query: The query to complete. Comes from the UI
- Returns: An observable with 0 or more results
*/
static func performQuery(query: String) -> Observable<[Result]> {
// Go to the network
// Get data
// Transform into Result objects
// If the returned Observable is disposed, it will cancel the network request.
}
}
/**
Some UI View Controller that has a UITextField on it
*/
class ViewController: UIViewController {
weak var textField: UITextField!
override func viewDidLoad() {
let o: Observable<[String]> =
textField.rx_text
.throttle(0.3)
.distinctUntilChanged()
.flatMapLatest { query in
Fetcher.performQuery(query)
}.map {
// Extract the text out of this object; standard Swift.
$0.map { $0.text }
}
// JAMIE: What are we doing with o??
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment