Skip to main content

RxSwiftからCombineに乗り換えた

Apple純正のCombineが出たと聞いたので乗り換えてみた


・RxSwiftでSingle

Single<AAA>.create { emitter in
    let url = hogehoge
    
    AFManager.request(url, method: .get).responseData(completionHandler: { [weak self] response in
        emitter(.success(response))
    })
    return Disposables.create()
}

・CombineでFuture

Future<AAA, Error> { promise in
    let url = hogehoge
    
    AFManager.request(url, method: .get).responseData(completionHandler: { [weak self] response in
        promise(.success(response))
    })
}

・RxSWfitで購読

private let disposeBag = DisposeBag()


.request()
.observe(on: MainScheduler.instance)
.subscribe { it in
}
.disposed(by: disposeBag)

・Combineで購読

private var cancellables: [AnyCancellable] = []


request()
    .sink(receiveCompletion: { [weak self] completion in
    }){ [weak self] it in
    }.store(in: &cancellables)

 

関連記事:

Pocket