Search code examples
iosswiftreactive-programmingrx-swift

How do I change observable type on my return method


im new in RxSwift, and this is what i want to achieve, hope you guys can help me out.

I have an observable that syncs my info with the server and caches the response and returns an Observable (succeded or failed), what i want to do is once i have this succeed code... go and get the information that is in the cache, but the error i keep getting is

Cannot convert value of type '(_) -> Observable' to expected argument type '((Bool) throws -> Void

I know that is because of my sync method returns a bool, but i dont know how can i trigger the cache pull info after the sync has happened.

Here is the code that im using...

private func fetchAndCacheMonthActivity(forDate date: Date) -> Observable<ActivityMonthInfo> {
        _activity.sync()
            .do(onNext: {[weak self] _ -> Observable<ActivityMonthInfo> in
                var cachedObs = self?._cacheMonthInfoObs[date.yearMonthString]

                guard cachedObs == nil else { return cachedObs! }

                cachedObs = _myActivitiesRepo.getMonthActivity(forMonthInDate: date)
                    .share(replay: 1, scope: SubjectLifetimeScope.forever)

                self?._cacheMonthInfoObs[date.yearMonthString] = cachedObs!

                return cachedObs!
            })
}

Thanks for reading... I hope you guys can help me out.


Solution

  • You should be using flatMap(_:) here rather than do(onNext:)

    But the code won't execute until you subscribe to it, so keep that in mind.