Skip to content

Instantly share code, notes, and snippets.

@muizidn
Created February 19, 2018 02:36
Show Gist options
  • Save muizidn/0855016cb40b7b4bf340db09b3788d93 to your computer and use it in GitHub Desktop.
Save muizidn/0855016cb40b7b4bf340db09b3788d93 to your computer and use it in GitHub Desktop.
RxSwift: Error handling without disposing subscription or Resubscribe after disposed
// Setelah print "Fetch Error ..." maka subscription -> onCompleted -> isDisposed (dan tidak bisa fetchProduct lagi)
Network.fetchProduct() // Return Observable<[Product]>
.subscribe(onNext: { value in
print("Product is \(value)")
}, onError: { error in
print("Fetch Error \(error)")
})
.diposed(by: bag)
// Jika pakai retry() maka "Fetch Error ..." tidak terpanggil karena 'dipantulkan' oleh retry()
Network.fetchProduct() // Return Observable<[Product]>
.retry()
.subscribe(onNext: { value in
print("Product is \(value)")
}, onError: { error in
print("Fetch Error \(error)") // onError menjadi tidak berguna
})
.diposed(by: bag)
// Yang saya inginkan
// Saat error saya bisa menampilkan error (onError terpanggil) namun subscription tidak diterminate (tidak onComplete)
// saat saya fetch lagi subscription mendapat value (onNext terpanggil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment