Skip to content

Commit 70bde6e

Browse files
committed
Added ObservableType extension to avoid Single requirement on RxSwift
1 parent 93de4d6 commit 70bde6e

3 files changed

Lines changed: 65 additions & 19 deletions

File tree

Moya-ObjectMapper.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = "Moya-ObjectMapper"
3-
s.version = "2.4.1"
3+
s.version = "2.4.2"
44
s.summary = "ObjectMapper bindings for Moya"
55
s.description = <<-EOS
66
[ObjectMapper](https://github.com/Hearst-DD/ObjectMapper) bindings for
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// ObserverType+ObjectMapper.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+
/// Extension for processing Responses into Mappable objects through ObjectMapper
14+
public extension ObservableType where E == Response {
15+
16+
/// Maps data received from the signal into an object
17+
/// which implements the Mappable protocol and returns the result back
18+
/// If the conversion fails, the signal errors.
19+
public func mapObject<T: BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Observable<T> {
20+
return flatMap { response -> Observable<T> in
21+
return Observable.just(try response.mapObject(T.self, context: context))
22+
}
23+
}
24+
25+
/// Maps data received from the signal into an array of objects
26+
/// which implement the Mappable protocol and returns the result back
27+
/// If the conversion fails, the signal errors.
28+
public func mapArray<T: BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Observable<[T]> {
29+
return flatMap { response -> Observable<[T]> in
30+
return Observable.just(try response.mapArray(T.self, context: context))
31+
}
32+
}
33+
}
34+
35+
36+
// MARK: - ImmutableMappable
37+
public extension ObservableType where E == Response {
38+
39+
/// Maps data received from the signal into an object
40+
/// which implements the ImmutableMappable protocol and returns the result back
41+
/// If the conversion fails, the signal errors.
42+
public func mapObject<T: ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Observable<T> {
43+
return flatMap { response -> Observable<T> in
44+
return Observable.just(try response.mapObject(T.self, context: context))
45+
}
46+
}
47+
48+
/// Maps data received from the signal into an array of objects
49+
/// which implement the ImmutableMappable protocol and returns the result back
50+
/// If the conversion fails, the signal errors.
51+
public func mapArray<T: ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Observable<[T]> {
52+
return flatMap { response -> Observable<[T]> in
53+
return Observable.just(try response.mapArray(T.self, context: context))
54+
}
55+
}
56+
}

Source/RxSwift/Single+ObjectMapper.swift

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,57 +10,47 @@ import RxSwift
1010
import Moya
1111
import ObjectMapper
1212

13-
1413
/// Extension for processing Responses into Mappable objects through ObjectMapper
1514
public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {
1615

1716
/// Maps data received from the signal into an object
1817
/// which implements the Mappable protocol and returns the result back
1918
/// 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
19+
public func mapObject<T: BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<T> {
20+
return flatMap { response -> Single<T> in
2321
return Single.just(try response.mapObject(type, context: context))
2422
}
25-
.observeOn(MainScheduler.instance)
2623
}
2724

2825
/// Maps data received from the signal into an array of objects
2926
/// which implement the Mappable protocol and returns the result back
3027
/// 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
28+
public func mapArray<T: BaseMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> {
29+
return flatMap { response -> Single<[T]> in
3430
return Single.just(try response.mapArray(type, context: context))
3531
}
36-
.observeOn(MainScheduler.instance)
3732
}
3833
}
3934

4035

4136
// MARK: - ImmutableMappable
42-
4337
public extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Response {
4438

4539
/// Maps data received from the signal into an object
4640
/// which implements the ImmutableMappable protocol and returns the result back
4741
/// 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
42+
public func mapObject<T: ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<T> {
43+
return flatMap { response -> Single<T> in
5144
return Single.just(try response.mapObject(type, context: context))
5245
}
53-
.observeOn(MainScheduler.instance)
5446
}
5547

5648
/// Maps data received from the signal into an array of objects
5749
/// which implement the ImmutableMappable protocol and returns the result back
5850
/// 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
51+
public func mapArray<T: ImmutableMappable>(_ type: T.Type, context: MapContext? = nil) -> Single<[T]> {
52+
return flatMap { response -> Single<[T]> in
6253
return Single.just(try response.mapArray(type, context: context))
6354
}
64-
.observeOn(MainScheduler.instance)
6555
}
6656
}

0 commit comments

Comments
 (0)