user4170419
user4170419

Reputation:

Chaining operations with RxSwift

I want to chain following operations

I wrote createUserandVerify as below. I wonder how should I write uploadStuff in reactive way. Upload function depends on user credentials. Therefore It must only run after createUserandVerify. I know I could just check count of array inside uploadStuff and return empty but I wonder the best practices.

func createUserandVerify() -> Single<User> {
    return Service.sharedInstance.generateAnonUser()
        .flatMap{ user in
            if Service.sharedInstance.isOldRegisteredUser {
                print("It is old user")
                // We need to verify the receipt
                return  Service.sharedInstance.verifyReceipt()
                    .flatMap { verifiedUser in
                        print("Returning Verified new user [Verification Success]")
                        return Single.just((verifiedUser))

                    }.catchError{ error ->Single<User> in
                        print("Returning firstly created user [Verification Failed]")
                        print("Error Type: \(error)")
                        return Single.just(user)

                }
            } else {
                //Normal anonymous old user
                print("Returning firstly created user [Anonymous]")
                return Single.just(user)
            }
    }
}

Upvotes: 3

Views: 1833

Answers (1)

christopher.online
christopher.online

Reputation: 2774

Assumptions (since I have not worked with Single I changed them to Observable):

func createUserandVerify() -> Observable<User>
func getStuff() -> [Stuff]
func uploadStuff(_ user: User) -> Observable<String> 

createUserandVerify() should publish errors with onError so uploadStuff will not be called if something goes wrong.

Possible solution:

enum CustomError: Error {
    case instanceMissing
    case notEnoughStuff
}

createUserandVerify()
    .flatMap { [weak self] (user) -> Observable<String> in
        guard let strongSelf = self else { throw CustomError.instanceMissing }
        guard strongSelf.getStuff().count > 0 else { throw CustomError.notEnoughStuff }
        return strongSelf.uploadStuff(user)
    }
    .subscribe(
        onNext: { stringResult in
          // print result from 'uploadStuff'
          print(stringResult)
    },
        onError: { error in
          // will be reached if something goes
          // wrong in 'createUserandVerify' or 'uploadStuff'
          // or if one of your custom errors in 'flatMap' are thrown
          print(error)
    })
    .disposed(by: disposeBag)

You could also make getStuff reactive by returning an Observable or Single and also include it in the chain via flatMap.

Upvotes: 2

Related Questions