Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxSwift: Chain Completable to Observable

I'd like to chain a Completable to an observable element. After calling flatMap, onCompleted and onError callbacks don't seem to be called on subscription.

var user = PublishRelay<User>()

func fetchUserInformation(_ userId: String) -> Completable {
    return Completable.create { observer in
        apiService.fetchInformation(for: userId, completion: { response in
            if let name = response?.name {
                user.accept(User(name: name))
                observer(.completed)
            } else {
                observer(.error(ServiceError.userInformation))
            }
        })
        return Disposables.create()
    }
}

login()
.flatMap{ userId in fetchUserInformation(userId) }
.subscribe(
    onCompleted: {
        print("Success!") // Not being called at all
    },
    onError: { error in
        print(error)  // Not being called at all
    }
).disposed(by: disposeBag)

Although fetchUserInformation and observer(.completed) are being called and user information is being successfully fetched, I won't be able to catch onCompleted on subscription (only when preceded by flatMap).

Is there a clean way to achieve this?

Already tried .materialized() just after the flatMap call in order to get an

    Observable<Event<Never>>

rather than a

    Observable<Never>

It doesn't work either.

like image 868
juliancadi Avatar asked Jun 08 '18 05:06

juliancadi


1 Answers

The correct solution would be using the ‘andThen’ operator.

someCompletable
   .andThen(someObservable) 

Edit: Just read the rest of your code - I'm not sure why you use a Completable at all since it seems you are actually returning some element from that stream.

You'll probably want to use a Single or Plain-ol' observable to relay that value without using an external Relay.

like image 70
Shai Mishali Avatar answered Sep 19 '22 14:09

Shai Mishali