Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxSwift Driver

RxSwift Driver

RxSwiftのDriverについてpotatotips #28で発表した際のスライドです http://connpass.com/dashboard/

Motoki Narita

April 20, 2016
Tweet

More Decks by Motoki Narita

Other Decks in Technology

Transcript

  1. validatedUsername = input.username .flatMapLatest { username in return validationService.validateUsername(username) .observeOn(MainScheduler.instance)

    .catchErrorJustReturn(.Failed(message: "Error contacting server")) } .shareReplay(1) %SJWFS͡Όͳ͍ύλʔϯ ೖྗ͞ΕͨϢʔβʔ໊ͷόϦσʔγϣϯΛߦ͍ͬͯΔՕॴ
  2. func validateUsername(username: String) -> Observable<ValidationResult> { if username.characters.count == 0

    { return Observable.just(.Empty) } // this obviously won't be if username.rangeOfCharacterFromSet(NSCharacterSet.alphanumericCharacterSet().invertedSet) != nil { return Observable.just(.Failed(message: "Username can only contain numbers or digits")) } let loadingValue = ValidationResult.Validating return API .usernameAvailable(username) .map { available in if available { return .OK(message: "Username available") } else { return .Failed(message: "Username already taken") } } .startWith(loadingValue) }
  3. enum ValidationResult { case OK(message: String) case Empty case Validating

    case Failed(message: String) } 7BMJEBUJPO3FTVMU
  4. validatedUsername = input.username .flatMapLatest { username in return validationService.validateUsername(username) .asDriver(onErrorJustReturn:

    .Failed(message: "Error contacting server")) } ൺֱ validatedUsername = input.username .flatMapLatest { username in return validationService.validateUsername(username) .observeOn(MainScheduler.instance) .catchErrorJustReturn(.Failed(message: "Error contacting server")) } .shareReplay(1)
  5. validatedUsername = input.username .flatMapLatest { username in return validationService.validateUsername(username) .asDriver(onErrorJustReturn:

    .Failed(message: "Error contacting server")) } validatedUsername = input.username .flatMapLatest { username in return validationService.validateUsername(username) .observeOn(MainScheduler.instance) .catchErrorJustReturn(.Failed(message: "Error contacting server")) } .shareReplay(1) ൺֱ