|
| 1 | +// |
| 2 | +// Single+MoyaObjectMapper.swift |
| 3 | +// |
| 4 | +// Created by Ivan Bruel on 09/12/15. |
| 5 | +// Copyright © 2015 Ivan Bruel. All rights reserved. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import RxSwift |
| 10 | +import Moya |
| 11 | +import ObjectMapper |
| 12 | + |
| 13 | + |
| 14 | +/// Extension for processing Responses into Mappable objects through ObjectMapper |
| 15 | +public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response { |
| 16 | + |
| 17 | + /// Maps data received from the signal into an object |
| 18 | + /// which implements the Mappable protocol and returns the result back |
| 19 | + /// If the conversion fails, the signal errors. |
| 20 | + public func mapObject<T:BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<T> { |
| 21 | + return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) |
| 22 | + .flatMap { response -> Single<T> in |
| 23 | + return Single.just(try response.mapObject(type, context: context)) |
| 24 | + } |
| 25 | + .observeOn(MainScheduler.instance) |
| 26 | + } |
| 27 | + |
| 28 | + /// Maps data received from the signal into an array of objects |
| 29 | + /// which implement the Mappable protocol and returns the result back |
| 30 | + /// If the conversion fails, the signal errors. |
| 31 | + public func mapArray<T:BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> { |
| 32 | + return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) |
| 33 | + .flatMap { response -> Single<[T]> in |
| 34 | + return Single.just(try response.mapArray(type, context: context)) |
| 35 | + } |
| 36 | + .observeOn(MainScheduler.instance) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +// MARK: - ImmutableMappable |
| 42 | + |
| 43 | +public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response { |
| 44 | + |
| 45 | + /// Maps data received from the signal into an object |
| 46 | + /// which implements the ImmutableMappable protocol and returns the result back |
| 47 | + /// If the conversion fails, the signal errors. |
| 48 | + public func mapObject<T:ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<T> { |
| 49 | + return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) |
| 50 | + .flatMap { response -> Single<T> in |
| 51 | + return Single.just(try response.mapObject(type, context: context)) |
| 52 | + } |
| 53 | + .observeOn(MainScheduler.instance) |
| 54 | + } |
| 55 | + |
| 56 | + /// Maps data received from the signal into an array of objects |
| 57 | + /// which implement the ImmutableMappable protocol and returns the result back |
| 58 | + /// If the conversion fails, the signal errors. |
| 59 | + public func mapArray<T:ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> { |
| 60 | + return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) |
| 61 | + .flatMap { response -> Single<[T]> in |
| 62 | + return Single.just(try response.mapArray(type, context: context)) |
| 63 | + } |
| 64 | + .observeOn(MainScheduler.instance) |
| 65 | + } |
| 66 | +} |
0 commit comments