Skip to content

Latest commit

 

History

History

lesson3.6_interval

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Lesson 3.6: interval

Problem statement

Write an app which spits out a "hello" message once per second, using func interval.

Problem project

You can use the project in the problem folder of this repo as a starting point.

Note: I have omitted the Carthage folder from the problem project, because it includes large binary files. In order to use the this project, you will need to run carthage update --platform iOS.

Solution

ViewController.swift:

import UIKit
import RxSwift
import RxCocoa

class TickHelloGenerator
{
    class func generate() -> Observable<String>
    {
        let tickerObservable = interval(1, MainScheduler.sharedInstance)

        let helloObservable = tickerObservable.map({ (_) -> String in
            return "hello"
        })
        
        return helloObservable
    }
}

class ViewController: UIViewController {

    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        TickHelloGenerator.generate().subscribeNext { (s) -> Void in
            debugPrint(s)
        }.addDisposableTo(disposeBag)
    }
}

Discussion:

Here, we use func interval to generate one event per second, which we then map into a "hello" message.

Start up the app and verify that you see one "hello" per second in the console:

"hello"
"hello"
"hello"
...

New concepts to explore

  • Open up RxExample.xcodeproj.
    • Take a look at func interval in Observable+Creation.swift

Solution project

My solution is included in the solution folder of this repo.

Note: I have omitted the Carthage folder from the solution project, because it includes large binary files. In order to run the this project, you will need to run carthage update --platform iOS.