The demo project for "Protocol-Oriented Programming in Networking".
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


This is the demo project for my presentation at try! Swift conference 2016.

Set Up

  • carthage bootstrap --platform iOS


  • Swift 3.0.1
  • Xcode 8.1


This demo project illustrates how to use RxSwift, Action and APIKit. The demo app fetches repositories via GitHub search API and displays them using the libraries.


PaginationViewModel<Element> is a view model for pagination. It has an initializer with type parameter Request, which is constrained to conform to PaginationRequest protocol. When PaginationViewModel<Element> is instantiated via init<Request>(baseRequest:), the type of its property that represents pagination elements will be inferred as Observable<[Request.Response.Element]>.

class PaginationViewModel<Element: Decodable> {
    let indicatorViewAnimating: Driver<Bool>
    let elements: Driver<[Element]>
    let loadError: Driver<Error>

    init<Request: PaginationRequest>(
        baseRequest: Request,
        viewWillAppear: Driver<Void>,
        scrollViewDidReachBottom: Driver<Void>) where Request.Response.Element == Element {...}


Once ViewModel is instantiated with a Request type parameter, remained task that ViewController have to do is binding input streams and output streams.

class SearchRepositoriesViewController: UITableViewController {
    @IBOutlet weak var indicatorView: UIActivityIndicatorView!

    private let disposeBag = DisposeBag()
    private var viewModel: PaginationViewModel<Repository>!

    override func viewDidLoad() {

        let baseRequest = GitHubAPI.SearchRepositoriesRequest(query: "Swift")

        viewModel = PaginationViewModel(
            baseRequest: baseRequest,
            viewWillAppear: rx.viewWillAppear.asDriver(),
            scrollViewDidReachBottom: tableView.rx.reachedBottom.asDriver())

   "Cell", cellType: RepositoryCell.self)),
   { print($0) }),