Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove scheduling from Single+ModelMapper #52

Merged
merged 2 commits into from
Feb 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions Sources/RxMoya-ModelMapper/Single+ModelMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,46 @@ extension PrimitiveSequence where TraitType == SingleTrait, ElementType == Respo
/// implements the Mappable protocol and returns the result back on the MainScheduler.
/// If the conversion fails, the signal errors.
public func map<T: Mappable>(to type: T.Type, keyPath: String? = nil) -> Single<T> {
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.flatMap { response -> Single<T> in
return 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 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<T: Mappable>(to type: [T].Type, keyPath: String? = nil) -> Single<[T]> {
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.flatMap { response -> Single<[T]> in
return 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<T: Mappable>(to type: T.Type, keyPath: String? = nil) -> Single<T?> {
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.flatMap { response -> Single<T?> in
return flatMap { response -> Single<T?> in
do {
let object = try response.map(to: type, keyPath: keyPath)
return Single.just(object)
} 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<T: Mappable>(to type: [T].Type, keyPath: String? = nil) -> Single<[T]?> {
return observeOn(ConcurrentDispatchQueueScheduler(qos: .background))
.flatMap { response -> Single<[T]?> in
return flatMap { response -> Single<[T]?> in
do {
let object = try response.map(to: type, keyPath: keyPath)
return Single.just(object)
} catch {
return Single.just(nil)
}
}
.observeOn(MainScheduler.instance)
}
}
}