I'm having 2 methods like this:
func rxGetAllTonicsForLanguage(language: Language) -> Observable<AnyObject?>
func saveTonics(list: [Tonic]) -> Observable<AnyObject?>
Now I want to first do the getAllTonics call and then with the result of that call I want to do the next action. So I thought this was something I could do with FlatMap. But I'm stuck I can't figure out how to chain these.
I tried like follows:
self.remoteService.rxGetAllTonicsForLanguage(language)
.subscribeOn(ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background))
.flatMap{tonics -> Observable<[Tonic]> in
print("Tonics: \(tonics)")
let x = tonics as! [Tonic]
return TonicAdapter.sharedInstance.saveTonics(x)
}.observeOn(MainScheduler.instance)
.subscribe({ e in
switch e {
case .Next(let element):
if let result = element as? String {
DDLogDebug("Saved something \(result)")
}
case .Error(let e):
DDLogError("Error in save tonics \(e)")
case .Completed:
DDLogDebug("Completed save tonics")
}
}
).addDisposableTo(self.disposeBag)
It gives me this error on the line of return TonicAdapter:
Cannot convert return expression of type 'Observable<AnyObject?>' (aka 'Observable<Optional<AnyObject>>') to return type 'Observable<[Tonic]>' (aka 'Observable<Array<Tonic>>')
I don't see the problem because both methods are returning Observables?