interval

  • 특정 주기마다 정수를 방출하는 Observable을 생성하고 싶을 때 사용한다.
  • 첫번째 파라미터로 방출 주기를 정하고, 두번째 파라미터에 스케줄러를 전달한다.
  • 구독할때 방출이 시작되므로 새로운 구독자가 추가될때마다 타이머가 개별로 실행된다.
Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
	.subscribe { print($0) } // next(0) -> next(1) -> next(2)...

timer

  • interval(_:scheduler:)처럼 특정 주기마다 정수를 방출하지만 지연시간에 따라 동작방식이 달라진다.
  • 지연시간은 구독이 시작되고 지연시간만큼 지난 뒤 방출이 시작도록 한다.
  • period의 경우 optional인데, 만약 생략할 경우 하나의 이벤트만 방출하고 종료된다.
Observable<Int>.timer(.seconds(3), period: .seconds(1), scheduler: MainScheduler.instance)
	.subscribe { print($0) } // 3초 후 next(0) -> next(1) -> next(2)...

timeout

  • 첫번째 파라미터로 전달되는 시간동안 새로운 이벤트가 방출되지 않으면 에러 이벤트가 방출된다.
  • 반대로 이 시간안에 새로운 이벤트가 방출되면 그대로 전달된다.
  • other 파라미터에는 지정된 시간동안 새로운 이벤트를 방출하지 않은 경우 새롭게 구독할 구독자를 입력한다.
let subject = PublishSubject<Int>()

subject.timeout(.seconds(3), scheduler: MainScheduler.instance)
	.subscribe { print($0) } // next(0) -> error(Sequence timeout.)

Observable<Int>.timer(.seconds(1), period: .seconds(5), scheduler: MainScheduler.instance)
	.subscribe(onNext: { subject.onNext($0) })

let subject2 = PublishSubject<Int>()

subject2.timeout(.seconds(3), other: Observable.just(100), scheduler: MainScheduler.instance)
	.subscribe { print($0) } // next(100) -> completed

Observable<Int>.interval(.seconds(5), scheduler: MainScheduler.instance)
	.subscribe(onNext: { subject2.onNext($0) })

delay

  • 구독자에게 전달될 이벤트를 지정된 시간만큼 지연시킨다.
  • 에러의 경우 지연없이 즉시 전달된다.
  • 구독 자체를 지연하는 것이 아니라 이벤트 전달만 지연한다.
Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
	.take(10)
	.debug()
	.delay(.seconds(5), scheduler: MainScheduler.instance)
	.subscribe { print($0) }
2019-12-13 17:55:55.164: Hello.playground:28 (__lldb_expr_9) -> subscribed
2019-12-13 17:55:56.270: Hello.playground:28 (__lldb_expr_9) -> Event next(0)
2019-12-13 17:55:57.269: Hello.playground:28 (__lldb_expr_9) -> Event next(1)
2019-12-13 17:55:58.270: Hello.playground:28 (__lldb_expr_9) -> Event next(2)
2019-12-13 17:55:59.269: Hello.playground:28 (__lldb_expr_9) -> Event next(3)
2019-12-13 17:56:00.269: Hello.playground:28 (__lldb_expr_9) -> Event next(4)
2019-12-13 17:56:01.269: Hello.playground:28 (__lldb_expr_9) -> Event next(5)
next(0)
2019-12-13 17:56:02.269: Hello.playground:28 (__lldb_expr_9) -> Event next(6)
next(1)
2019-12-13 17:56:03.270: Hello.playground:28 (__lldb_expr_9) -> Event next(7)
next(2)
2019-12-13 17:56:04.269: Hello.playground:28 (__lldb_expr_9) -> Event next(8)
next(3)
2019-12-13 17:56:05.269: Hello.playground:28 (__lldb_expr_9) -> Event next(9)
2019-12-13 17:56:05.269: Hello.playground:28 (__lldb_expr_9) -> Event completed
2019-12-13 17:56:05.269: Hello.playground:28 (__lldb_expr_9) -> isDisposed
next(4)
next(5)
next(6)
next(7)
next(8)
next(9)
completed

delaySubscription

  • delay(_:scheduler:)와 달리 구독 자체를 지연시키고 싶은 경우에 사용한다.
Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
	.take(10)
	.debug()
	.delaySubscription(.seconds(7), scheduler: MainScheduler.instance)
	.subscribe { print($0) }
2019-12-13 17:58:58.423: Hello.playground:37 (__lldb_expr_11) -> subscribed
2019-12-13 17:58:59.427: Hello.playground:37 (__lldb_expr_11) -> Event next(0)
next(0)
2019-12-13 17:59:00.427: Hello.playground:37 (__lldb_expr_11) -> Event next(1)
next(1)
2019-12-13 17:59:01.427: Hello.playground:37 (__lldb_expr_11) -> Event next(2)
next(2)
2019-12-13 17:59:02.427: Hello.playground:37 (__lldb_expr_11) -> Event next(3)
next(3)
2019-12-13 17:59:03.428: Hello.playground:37 (__lldb_expr_11) -> Event next(4)
next(4)
2019-12-13 17:59:04.427: Hello.playground:37 (__lldb_expr_11) -> Event next(5)
next(5)
2019-12-13 17:59:05.429: Hello.playground:37 (__lldb_expr_11) -> Event next(6)
next(6)
2019-12-13 17:59:06.428: Hello.playground:37 (__lldb_expr_11) -> Event next(7)
next(7)
2019-12-13 17:59:07.429: Hello.playground:37 (__lldb_expr_11) -> Event next(8)
next(8)
2019-12-13 17:59:08.428: Hello.playground:37 (__lldb_expr_11) -> Event next(9)
next(9)
2019-12-13 17:59:08.429: Hello.playground:37 (__lldb_expr_11) -> Event completed
completed
2019-12-13 17:59:08.429: Hello.playground:37 (__lldb_expr_11) -> isDisposed