From f398e484e226a98734af10b3f3a54ef0fadba34c Mon Sep 17 00:00:00 2001 From: Ivan Cheung Date: Tue, 27 Feb 2018 18:01:07 +0900 Subject: [PATCH 1/2] Single+ModelMapper: Add parameters to allow injection of a scheduler --- .../Single+ModelMapper.swift | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift b/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift index 82b1148..15bbb5f 100644 --- a/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift +++ b/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift @@ -20,30 +20,28 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Maps data received from the signal into an object (on the default Background thread) which /// implements the Mappable protocol and returns the result back on the MainScheduler. /// If the conversion fails, the signal errors. - public func map(to type: T.Type, keyPath: String? = nil) -> Single { - return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) + public func map(to type: T.Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single { + return self.observeOn(scheduler) .flatMap { response -> Single in return Single.just(try response.map(to: type, keyPath: keyPath)) - } - .observeOn(MainScheduler.instance) + } } - + /// Maps data received from the signal into an array of objects (on the default Background thread) /// which implement the Mappable protocol and returns the result back on the MainScheduler /// If the conversion fails, the signal errors. - public func map(to type: [T].Type, keyPath: String? = nil) -> Single<[T]> { - return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) + public func map(to type: [T].Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single<[T]> { + return self.observeOn(scheduler) .flatMap { response -> Single<[T]> in return Single.just(try response.map(to: type, keyPath: keyPath)) - } - .observeOn(MainScheduler.instance) + } } - + /// Maps data received from the signal into an object (on the default Background thread) which /// implements the Mappable protocol and returns the result back on the MainScheduler. /// If the conversion fails, the nil is returned instead of error signal. - public func mapOptional(to type: T.Type, keyPath: String? = nil) -> Single { - return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) + public func mapOptional(to type: T.Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single { + return self.observeOn(scheduler) .flatMap { response -> Single in do { let object = try response.map(to: type, keyPath: keyPath) @@ -51,16 +49,14 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo } catch { return Single.just(nil) } - - } - .observeOn(MainScheduler.instance) + } } - + /// Maps data received from the signal into an array of objects (on the default Background thread) /// which implement the Mappable protocol and returns the result back on the MainScheduler /// If the conversion fails, the nil is returned instead of error signal. - public func mapOptional(to type: [T].Type, keyPath: String? = nil) -> Single<[T]?> { - return observeOn(ConcurrentDispatchQueueScheduler(qos: .background)) + public func mapOptional(to type: [T].Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single<[T]?> { + return self.observeOn(scheduler) .flatMap { response -> Single<[T]?> in do { let object = try response.map(to: type, keyPath: keyPath) @@ -68,7 +64,7 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo } catch { return Single.just(nil) } - } - .observeOn(MainScheduler.instance) + } } } + From 57f4fefb6ea5733be8fc1af6b0ae7caf7be39bde Mon Sep 17 00:00:00 2001 From: Ivan Cheung Date: Tue, 27 Feb 2018 18:41:11 +0900 Subject: [PATCH 2/2] Single+ModelMapper: Removed scheduler injection in favour of deferring scheduling to the call site --- .../Single+ModelMapper.swift | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift b/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift index 15bbb5f..c546770 100644 --- a/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift +++ b/Sources/RxMoya-ModelMapper/Single+ModelMapper.swift @@ -20,9 +20,8 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Maps data received from the signal into an object (on the default Background thread) which /// implements the Mappable protocol and returns the result back on the MainScheduler. /// If the conversion fails, the signal errors. - public func map(to type: T.Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single { - return self.observeOn(scheduler) - .flatMap { response -> Single in + public func map(to type: T.Type, keyPath: String? = nil) -> Single { + return flatMap { response -> Single in return Single.just(try response.map(to: type, keyPath: keyPath)) } } @@ -30,9 +29,8 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Maps data received from the signal into an array of objects (on the default Background thread) /// which implement the Mappable protocol and returns the result back on the MainScheduler /// If the conversion fails, the signal errors. - public func map(to type: [T].Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single<[T]> { - return self.observeOn(scheduler) - .flatMap { response -> Single<[T]> in + public func map(to type: [T].Type, keyPath: String? = nil) -> Single<[T]> { + return flatMap { response -> Single<[T]> in return Single.just(try response.map(to: type, keyPath: keyPath)) } } @@ -40,9 +38,8 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Maps data received from the signal into an object (on the default Background thread) which /// implements the Mappable protocol and returns the result back on the MainScheduler. /// If the conversion fails, the nil is returned instead of error signal. - public func mapOptional(to type: T.Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single { - return self.observeOn(scheduler) - .flatMap { response -> Single in + public func mapOptional(to type: T.Type, keyPath: String? = nil) -> Single { + return flatMap { response -> Single in do { let object = try response.map(to: type, keyPath: keyPath) return Single.just(object) @@ -55,9 +52,8 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo /// Maps data received from the signal into an array of objects (on the default Background thread) /// which implement the Mappable protocol and returns the result back on the MainScheduler /// If the conversion fails, the nil is returned instead of error signal. - public func mapOptional(to type: [T].Type, scheduler: SchedulerType, keyPath: String? = nil) -> Single<[T]?> { - return self.observeOn(scheduler) - .flatMap { response -> Single<[T]?> in + public func mapOptional(to type: [T].Type, keyPath: String? = nil) -> Single<[T]?> { + return flatMap { response -> Single<[T]?> in do { let object = try response.map(to: type, keyPath: keyPath) return Single.just(object)