[Swift] Observable 기본 문법 정리 2

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

공통 : RxSwift import, DisposeBag 인스턴스 생성, Error 정의

import RxSwift
import Foundation

let disposeBag = DisposeBag()

enum TraitsError: Error {
    case single
    case maybe
    case completable
}

입력

print("-------Single1-------")
Single<String>.just("✅")
    .subscribe(
        onSuccess: {
            print($0)
        },
        onFailure: {
            print("error: \($0)")
        },
        onDisposed: {
            print("disposed")
        })
    .disposed(by: disposeBag)

//Observable<String>.just("✅")
//    .subscribe(
//        onNext: {},
//        onError: {},
//        onCompleted: {},
//        onDisposed: {}
//    )

출력

-------Single1-------
✅
disposed

입력

print("-------Single2-------")
Observable<String>.create { observer -> Disposable in
    observer.onError(TraitsError.single)
    return Disposables.create()
    }
    .asSingle()
    .subscribe(
        onSuccess: {
            print($0)
        },
        onFailure: {
            print("error: \($0.localizedDescription)")
        },
        onDisposed: {
            print("disposed")
        })
    .disposed(by: disposeBag)

출력

-------Single2-------
error: The operation couldn’t be completed. (__lldb_expr_73.TraitsError error 0.)
disposed

입력

print("-------Single3-------")
struct SomeJSON: Decodable {
    let name: String
}

enum JSONError: Error {
    case decodingError
}

let json1 = """
    {"name":"Junsu"}
"""
let json2 = """
    {"my_name":"Jang"}
"""

func decode(json: String) -> Single<SomeJSON> {
    Single<SomeJSON>.create { observer -> Disposable in
        guard let data = json.data(using: .utf8),
              let json = try? JSONDecoder().decode(SomeJSON.self, from: data) else {
            observer(.failure(JSONError.decodingError))
            return Disposables.create()
        }
        
        observer(.success(json))
        return Disposables.create()
    }
}

decode(json: json1)
    .subscribe {
        switch $0 {
        case .success(let json):
            print(json.name)
        case .failure(let error):
            print(error)
        }
    }
    .disposed(by: disposeBag)

decode(json: json2)
    .subscribe {
        switch $0 {
        case .success(let json):
            print(json.name)
        case .failure(let error):
            print(error)
        }
    }
    .disposed(by: disposeBag)

출력

-------Single3-------
Junsu
decodingError

입력

print("-------Maybe1-------")
Maybe<String>.just("✅")
    .subscribe(
        onSuccess: {
            print($0)
        },
        onError: {
            print($0)
        },
        onCompleted: {
            print("completed")
        },
        onDisposed: {
            print("disposed")
        }
    )
    .disposed(by: disposeBag)

출력

-------Maybe1-------
✅
disposed

입력

print("-------Maybe2-------")
let t = Observable<String>.create { observer -> Disposable in
    observer.onError(TraitsError.maybe)
    return Disposables.create()
}
    .asMaybe()
    .subscribe(
        onSuccess: {
            print("성공: \($0)")
        },
        onError: {
            print("에러: \($0)")
        },
        onCompleted: {
            print("completed")
        },
        onDisposed: {
            print("disposed")
        }
    )
    .disposed(by: disposeBag)

출력

-------Maybe2-------
에러: maybe
disposed

입력

print("-------Completable1-------")
Completable.create { observer -> Disposable in
    observer(.error(TraitsError.completable))
    return Disposables.create()
}
.subscribe(
    onCompleted: {
        print("completed")
    },
    onError: {
        print("error: \($0)")
    },
    onDisposed: {
        print("disposed")
    }
)
.disposed(by: disposeBag)

출력

-------Completable1-------
error: completable
disposed

입력

print("-------Completable2-------")
Completable.create { observer -> Disposable in
    observer(.completed)
    return Disposables.create()
}
.subscribe {
    print($0)
}
.disposed(by: disposeBag)

출력

-------Completable2-------
completed
profile
🤭Swift My Velog🤭

0개의 댓글