[Swift] Observable 기본 문법 정리

준수·2022년 10월 17일
0
post-thumbnail

입력

print("----Just----")
Observable<Int>.just(1)
    .subscribe(onNext: {
        print($0)
    })
// 한개의 값 전달

출력

----Just----
1

입력

print("----Of----")
Observable<Int>.of(1, 2, 3, 4, 5)
    .subscribe(onNext: {
        print($0)
    })
// 한개 또는 여러개의 값 전달

출력

----Of----
1
2
3
4
5

입력

print("----Of2----")
Observable.of([1, 2, 3, 4, 5])
    .subscribe(onNext: {
        print($0)
    })
// just와 같음

출력

----Of2----
[1, 2, 3, 4, 5]

입력

print("----From----")
Observable.from([1, 2, 3, 4, 5])
    .subscribe(onNext: {
        print($0)
    })
// Array로 된 값을 하나씩 방출

출력

----From----
1
2
3
4
5

입력

print("--------subscribe1--------")
Observable.of(1, 2, 3)
    .subscribe {
        print($0)
    }

출력

--------subscribe1--------
next(1)
next(2)
next(3)
completed

입력

print("--------subscribe2--------")
Observable.of(1, 2, 3)
    .subscribe {
        if let element = $0.element {
            print(element)
        }
    }

출력

--------subscribe2--------
1
2
3

입력

print("--------subscribe3--------")
Observable.of(1, 2, 3)
    .subscribe(onNext: {
        print($0)
    })

출력

--------subscribe3--------
1
2
3

입력

print("--------empty--------")
Observable<Void>.empty()
    .subscribe {
        print($0)
    }

출력

--------empty--------
completed

입력

print("--------never--------")
Observable<Void>.never()
    .debug("never")
    .subscribe(
        onNext: {
            print($0)
        },
        onCompleted: {
            print("COMPLETED")
        }
    )

출력

--------never--------
2022-10-18 00:24:22.282: never -> subscribed

입력

print("--------range--------")
Observable.range(start: 1, count: 9)
    .subscribe(onNext: {
        print("2*\($0)=\(2*$0)")
    })

출력

--------range--------
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18

입력

print("--------dispose--------")
Observable.of(1, 2, 3)
    .subscribe {
        print($0)
    }
    .dispose()

출력

--------dispose--------
next(1)
next(2)
next(3)
completed

입력

print("--------disposeBag--------")
let disposeBag = DisposeBag()

Observable.of(1, 2, 3)
    .subscribe {
        print($0)
    }
    .disposed(by: disposeBag)

출력

--------disposeBag--------
next(1)
next(2)
next(3)
completed

입력

print("--------create1--------")
Observable.create { observer -> Disposable in
    observer.onNext(1)
    observer.onCompleted()
    observer.onNext(2)
    return Disposables.create()
}
.subscribe {
    print($0)
}
.disposed(by: disposeBag)

출력

--------create1--------
1

입력

print("--------create2--------")
enum MyError: Error {
    case anError
}

Observable.create { observer -> Disposable in
    observer.onNext(1)
    observer.onError(MyError.anError)
    observer.onCompleted()
    observer.onNext(2)
    return Disposables.create()
}
.subscribe(
    onNext: {
        print($0)
    },
    onError: {
        print($0.localizedDescription)
    },
    onCompleted: {
        print("completed")
    },
    onDisposed: {
        print("disposed")
    }
)
.disposed(by: disposeBag)

출력

--------create2--------
1
The operation couldn’t be completed. (__lldb_expr_71.MyError error 0.)
disposed

입력

print("--------deffered1--------")
Observable.deferred {
    Observable.of(1, 2, 3)
}
.subscribe {
    print($0)
}
.disposed(by: disposeBag)

출력

--------deffered1--------
next(1)
next(2)
next(3)
completed

입력

print("--------deffered2--------")
var toggler: Bool = false

let toggleFactory: Observable<String> = Observable.deferred {
    toggler.toggle()
    
    if toggler {
        return Observable.just("🫳down")
    } else {
        return Observable.just("🫴up")
    }
}

for _ in 0...3 {
    toggleFactory.subscribe(onNext: {
        print($0)
    })
    .disposed(by: disposeBag)
}

출력

--------deffered2--------
🫳down
🫴up
🫳down
🫴up
profile
🤭Swift My Velog🤭

0개의 댓글