Forked from jamiepinkham/gist:cccbcb8c12bfa705c76137b7a086b7fe
Last active
April 5, 2016 13:44
-
-
Save cliss/b8bee47a1c4f8b7d59ae48cb2cb34d7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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