RxSwift) Operators (2)

Havi·2021년 3월 6일
0

RxSwift 기초

목록 보기
6/14

Transforming Observables

Map

Map Operator는 ElementResult로 바꾸며, Event<Result>를 방출한다.

구현체

extension Event {

    /// Maps sequence elements using transform. If error happens during the transform, `.error`
    /// will be returned as value.
    public func map<Result>(_ transform: (Element) throws -> Result) -> RxSwift.Event<Result>
}

마블

예제

FlatMap

민소네님 블로그 참고

어떠한 이벤트가 왔을 때 그 이벤트를 다른 시퀀스로 바꿔준다.

구현체

extension InfallibleType {

    /**
         Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
    
         - seealso: [flatMap operator on reactivex.io](http://reactivex.io/documentation/operators/flatmap.html)
    
         - parameter selector: A transform function to apply to each element.
         - returns: An observable sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence.
         */
    public func flatMap<Source>(_ selector: @escaping (Self.Element) -> Source) -> RxSwift.Infallible<Source.Element> where Source : RxSwift.ObservableConvertibleType

    /**
         Projects each element of an observable sequence into a new sequence of observable sequences and then
         transforms an observable sequence of observable sequences into an observable sequence producing values only from the most recent observable sequence.
    
         It is a combination of `map` + `switchLatest` operator
    
         - seealso: [flatMapLatest operator on reactivex.io](http://reactivex.io/documentation/operators/flatmap.html)
    
         - parameter selector: A transform function to apply to each element.
         - returns: An observable sequence whose elements are the result of invoking the transform function on each element of source producing an
         Observable of Observable sequences and that at any point in time produces the elements of the most recent inner observable sequence that has been received.
         */
    public func flatMapLatest<Source>(_ selector: @escaping (Self.Element) -> Source) -> RxSwift.Infallible<Source.Element> where Source : RxSwift.ObservableConvertibleType

    /**
         Projects each element of an observable sequence to an observable sequence and merges the resulting observable sequences into one observable sequence.
         If element is received while there is some projected observable sequence being merged it will simply be ignored.
    
         - seealso: [flatMapFirst operator on reactivex.io](http://reactivex.io/documentation/operators/flatmap.html)
    
         - parameter selector: A transform function to apply to element that was observed while no observable is executing in parallel.
         - returns: An observable sequence whose elements are the result of invoking the one-to-many transform function on each element of the input sequence that was received while no other sequence was being calculated.
         */
    public func flatMapFirst<Source>(_ selector: @escaping (Self.Element) -> Source) -> RxSwift.Infallible<Source.Element> where Source : RxSwift.ObservableConvertibleType
}

마블

FlatmapFirst

이벤트를 다른 시퀀스로 바꿨을 때 다음 이벤트가 왔을 경우 무시하고 기존 이벤트에서 온 시퀀스를 진행한다.

FlatmapLatest

이벤트를 다른 시퀀스로 바꿨을 때 다음 이벤트가 오면 기존 이벤트 시퀀스를 무시하고 다음 이벤트를 새로운 시퀀스로 바꿔서 진행한다.

예제

Observable<Int>.of(1, 2, 3)
            .flatMap { (item: Int) -> Observable<String> in
                print(item)
                let flatMap = Observable<String>.of("\(item) : one", "\(item) : two")
                return flatMap
            }
        .subscribe(onNext: {
            print($0)
        })
        .disposed(by: disposeBag)
        /*prints
        1
        1 : one
        2
        1 : two
        2 : one
        3
        2 : two
        3 : one
        3 : two
*/

compactMap

Optinal한 값을 Result타입으로 바꿔준다.

구현체

extension ObservableType {

    /**
         Projects each element of an observable sequence into an optional form and filters all optional results.
    
         - parameter transform: A transform function to apply to each source element and which returns an element or nil.
         - returns: An observable sequence whose elements are the result of filtering the transform function for each element of the source.
    
         */
    public func compactMap<Result>(_ transform: @escaping (Self.Element) throws -> Result?) -> RxSwift.Observable<Result>
}

예제

Observable<Int?>.of(Optional(1), Optional(2), Optional(3))
            .compactMap { $0 }
//            .map { (item :Int?) -> Int in
//                return item!
//            }
            .subscribe(onNext: {
                print($0)
            })
            .disposed(by: disposeBag)

GroupBy

구현체

extension ObservableType {

    public func groupBy<Key>(keySelector: @escaping (Self.Element) throws -> Key) -> RxSwift.Observable<RxSwift.GroupedObservable<Key, Self.Element>> where Key : Hashable
}

마블

Buffer

구현체

extension ObservableType {

    /**
         Projects each element of an observable sequence into a buffer that's sent out when either it's full or a given amount of time has elapsed, using the specified scheduler to run timers.
    
         A useful real-world analogy of this overload is the behavior of a ferry leaving the dock when all seats are taken, or at the scheduled time of departure, whichever event occurs first.
    
         - seealso: [buffer operator on reactivex.io](http://reactivex.io/documentation/operators/buffer.html)
    
         - parameter timeSpan: Maximum time length of a buffer.
         - parameter count: Maximum element count of a buffer.
         - parameter scheduler: Scheduler to run buffering timers on.
         - returns: An observable sequence of buffers.
         */
    public func buffer(timeSpan: RxSwift.RxTimeInterval, count: Int, scheduler: RxSwift.SchedulerType) -> RxSwift.Observable<[Self.Element]>
}

마블

profile
iOS Developer

0개의 댓글