Tunko Development Diary

RxSwift) Time-based Operators(delay, delaySubscription) 본문

Development/RxSwift

RxSwift) Time-based Operators(delay, delaySubscription)

Tunko 2022. 6. 7. 16:45

delay

delay연산자는 Next 이벤트의 전달을 지연시킵니다.

Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
    .take(10)
    .debug()
    .delay(.seconds(5), scheduler: MainScheduler.instance)
    .subscribe { print(Date(), $0)}
    .disposed(by: bag)

출력

2022-06-07 16:35:05.311: delay.playground:41 (__lldb_expr_36) -> subscribed
2022-06-07 16:35:06.314: delay.playground:41 (__lldb_expr_36) -> Event next(0)
2022-06-07 16:35:07.313: delay.playground:41 (__lldb_expr_36) -> Event next(1)
2022-06-07 16:35:08.314: delay.playground:41 (__lldb_expr_36) -> Event next(2)
2022-06-07 16:35:09.314: delay.playground:41 (__lldb_expr_36) -> Event next(3)
2022-06-07 16:35:10.314: delay.playground:41 (__lldb_expr_36) -> Event next(4)
2022-06-07 16:35:11.313: delay.playground:41 (__lldb_expr_36) -> Event next(5)
2022-06-07 07:35:11 +0000 next(0)
2022-06-07 16:35:12.313: delay.playground:41 (__lldb_expr_36) -> Event next(6)
2022-06-07 07:35:12 +0000 next(1)
2022-06-07 16:35:13.313: delay.playground:41 (__lldb_expr_36) -> Event next(7)
2022-06-07 07:35:13 +0000 next(2)
2022-06-07 16:35:14.314: delay.playground:41 (__lldb_expr_36) -> Event next(8)
2022-06-07 07:35:14 +0000 next(3)
2022-06-07 16:35:15.314: delay.playground:41 (__lldb_expr_36) -> Event next(9)
2022-06-07 16:35:15.314: delay.playground:41 (__lldb_expr_36) -> Event completed
2022-06-07 16:35:15.314: delay.playground:41 (__lldb_expr_36) -> isDisposed
2022-06-07 07:35:15 +0000 next(4)
2022-06-07 07:35:16 +0000 next(5)
2022-06-07 07:35:17 +0000 next(6)
2022-06-07 07:35:18 +0000 next(7)
2022-06-07 07:35:19 +0000 next(8)
2022-06-07 07:35:20 +0000 next(9)
2022-06-07 07:35:20 +0000 completed

로그를 보면 알 수 있습니다.

debug 연산자를 사용하여 내부적으로 발생하는 로그를 확인했습니다.

처음 5초동안은 next 이벤트가 발생했지만 지연 후

take(10) 만큼 이벤트가 전달됩니다.

delaySubscription

delaySubscription 연산자는 구독 하는 시점을 지연시킵니다.

print("시작시간 : \\(Date())")
Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
    .take(10)
    .debug()
    .delaySubscription(.seconds(3), scheduler: MainScheduler.instance)
    .subscribe { print(Date(), $0)}
    .disposed(by: bag)

출력

시작시간 : 2022-06-07 07:41:17 +0000
2022-06-07 16:41:20.875: delaySubscription.playground:42 (__lldb_expr_40) -> subscribed
2022-06-07 16:41:21.878: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(0)
2022-06-07 07:41:21 +0000 next(0)
2022-06-07 16:41:22.878: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(1)
2022-06-07 07:41:22 +0000 next(1)
2022-06-07 16:41:23.877: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(2)
2022-06-07 07:41:23 +0000 next(2)
2022-06-07 16:41:24.877: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(3)
2022-06-07 07:41:24 +0000 next(3)
2022-06-07 16:41:25.877: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(4)
2022-06-07 07:41:25 +0000 next(4)
2022-06-07 16:41:26.877: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(5)
2022-06-07 07:41:26 +0000 next(5)
2022-06-07 16:41:27.876: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(6)
2022-06-07 07:41:27 +0000 next(6)
2022-06-07 16:41:28.877: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(7)
2022-06-07 07:41:28 +0000 next(7)
2022-06-07 16:41:29.876: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(8)
2022-06-07 07:41:29 +0000 next(8)
2022-06-07 16:41:30.876: delaySubscription.playground:42 (__lldb_expr_40) -> Event next(9)
2022-06-07 07:41:30 +0000 next(9)
2022-06-07 16:41:30.876: delaySubscription.playground:42 (__lldb_expr_40) -> Event completed
2022-06-07 07:41:30 +0000 completed
2022-06-07 16:41:30.876: delaySubscription.playground:42 (__lldb_expr_40) -> isDisposed

시작시간 로그와 이후 처음 next이벤트 방출 시간을 확인하면 구독하는 시간이 3초 정도의 오차로 지연된것을 확인할 수 있습니다.

반응형
Comments