Created
January 5, 2021 02:42
-
-
Save brownsoo/4f5abf5ee14ed148eab9306c4c28d05d to your computer and use it in GitHub Desktop.
Combine publishers to sequence result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func fetchCoachingRoom(complete: @escaping (Error?) -> Void) { | |
refreshCoachingRoomStatus = .working | |
cancellableCoachingRoom?.cancel() | |
cancellableCoachingRoom = apiService.stream(Api.getCoachingRoom()) | |
.map({ (res) -> [AClass] in | |
return res.userClassList | |
}) | |
.receive(on: DispatchQueue.main) | |
.map { [weak self] list -> [Int] in | |
self?.coachingRoomClasses = list | |
return list.compactMap { $0._id } | |
} | |
.receive(on: DispatchQueue.global()) | |
.flatMap { (idList) -> AnyPublisher<[UserClassResponse], Error> in | |
let requests = idList.map { Webservice.shared.stream(Api.getUserClass(classId: $0)) } | |
let publisherOfPublishers = Publishers.Sequence<[AnyPublisher<UserClassResponse, Error>], Error>(sequence: requests) | |
HLog.d("getCoachingRoom--1") | |
return publisherOfPublishers.flatMap { $0 }.collect().eraseToAnyPublisher() | |
} | |
.subscribe(on: DispatchQueue.global()) | |
.receive(on: DispatchQueue.main) | |
.sink(receiveCompletion: { [weak self] (completion) in | |
HLog.d("getCoachingRoom--2") | |
switch completion { | |
case .failure(let error): | |
self?.refreshCoachingRoomStatus = .notYet | |
complete(error) | |
case .finished: | |
self?.refreshCoachingRoomStatus = .done | |
complete(nil) | |
} | |
}, receiveValue: { [weak self] (classes) in | |
self?.handleAllUserClassResponseList(list: classes) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment