Skip to content

Instantly share code, notes, and snippets.

@mingyeow
Created August 11, 2016 21:45
Show Gist options
  • Save mingyeow/0634e09d090e87e32791984fe3ce4825 to your computer and use it in GitHub Desktop.
Save mingyeow/0634e09d090e87e32791984fe3ce4825 to your computer and use it in GitHub Desktop.
Comparing ReactiveKit and RxSwift Error Handling
// In ReactiveKit, there is a function to emit an completed and handle the error seperately, allowing the eventual subscriber to only worry about the success cases
import ReactiveKit
func rkFunction(arg:Int) -> Operation<Int, NSError>{
return Operation<Int, NSError> { observer in
observer.next(arg * 2)
observer.failure(NSError(domain: "", code: 1, userInfo: nil))
return NotDisposable
}
}
var stream = Stream.sequence([1, 2, 3])
var feed = stream.flatMapLatest { i in
return rkFunction(i).toStream(justLogError: true)
}
// ONLY: 2, 4, 6
// When the flatmap operation hits an error, the subscription is only receiving the successes
// And is able to continue without having to deal with errors (The operation BOTH continues without anything passed on to the subscriber (the error is logged)
feed.observeNext { i in
print("RESULT: \(i)")
}
// RESULT: 2
// Operation at /var/folders/3n/44zcn9b92yd7v2dlh_svvpyw0000gn/T/lldb/58657/playground7.swift:23 encountered an error: Error Domain= Code=1 "(null)"
// RESULT: 4
// Operation at /var/folders/3n/44zcn9b92yd7v2dlh_svvpyw0000gn/T/lldb/58657/playground7.swift:23 encountered an error: Error Domain= Code=1 "(null)"
// RESULT: 6
// Operation at /var/folders/3n/44zcn9b92yd7v2dlh_svvpyw0000gn/T/lldb/58657/playground7.swift:23 encountered an error: Error Domain= Code=1 "(null)"
// this is the function that wraps the error in a completion
// public func toStream(justLogError logError: Bool, completeOnError: Bool = false, file: String = #file, line: Int = #line) -> Stream<Element> {
// return Stream { observer in
// return self.observe { event in
// switch event {
// case .Next(let element):
// observer.next(element)
// case .Failure(let error):
// if completeOnError {
// observer.completed()
// }
// if logError {
// print("Operation at \(file):\(line) encountered an error: \(error)")
// }
// case .Completed:
// observer.completed()
// }
// }
// }
// }
// In RXSWIFT, the flatmap has to handle the error by passing on observable, otherwise future signals will not trigger it. See examples below.
// This means that we often need to bundle the result in an optional or result type, and handle it in the flatmap catcherror
import RxSwift
enum AwfulError: ErrorType { case Bad}
// returns an observable that emits an int, then error
func rxFunction(arg:Int) -> Observable<Int>{
let k = Observable<Int>.create({ observer -> Disposable in
observer.onNext(arg * 2)
observer.onError(AwfulError.Bad)
return NopDisposable.instance
})
return k
}
var l = [1, 2, 3].toObservable()
var feedRxWithoutHandling = l.flatMapLatest { i in
return rxFunction(i)
}
// 2, <END>
feedRxWithoutHandling.subscribeNext{ i in print("RESULT (NO HANDLE): \(i)") }
var feedRxJustReturn = l.flatMapLatest { i in
return rxFunction(i).catchErrorJustReturn(0)
}
// 2, 0, 4, 0, 6, 0 <END>
feedRxJustReturn.subscribeNext{ i in print("RESULT (JUST RETURN): \(i)") }
var feedRxHandleError = l.flatMapLatest { i in
return rxFunction(i).catchError({ e in
print (e)
return Observable.just(1)
})
}
// 2, 1, 4, 1, 6, 1
feedRxHandleError.subscribeNext{ i in print("RESULT (HANDLE ERROR): \(i)")}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment