Skip to content

Instantly share code, notes, and snippets.

@satishbabariya
Created July 2, 2018 12:04
Show Gist options
  • Save satishbabariya/d980924d9ce4a59c1f851d9227c669a3 to your computer and use it in GitHub Desktop.
Save satishbabariya/d980924d9ce4a59c1f851d9227c669a3 to your computer and use it in GitHub Desktop.
ReachabilityService
import RxSwift
import Foundation
import Reachability
public enum ReachabilityStatus {
case reachable(viaWiFi: Bool)
case unreachable
}
extension ReachabilityStatus {
var reachable: Bool {
switch self {
case .reachable:
return true
case .unreachable:
return false
}
}
}
protocol ReachabilityService {
var reachability: Observable<ReachabilityStatus> { get }
}
enum ReachabilityServiceError: Error {
case failedToCreate
}
class DefaultReachabilityService: ReachabilityService {
fileprivate let _reachabilitySubject: BehaviorSubject<ReachabilityStatus>
var reachability: Observable<ReachabilityStatus> {
return self._reachabilitySubject.asObservable()
}
let _reachability: Reachability
init() throws {
guard let reachabilityRef = Reachability() else { throw ReachabilityServiceError.failedToCreate }
let reachabilitySubject = BehaviorSubject<ReachabilityStatus>(value: .unreachable)
// so main thread isn't blocked when reachability via WiFi is checked
let backgroundQueue = DispatchQueue(label: "reachability.wificheck")
reachabilityRef.whenReachable = { _ in
backgroundQueue.async {
reachabilitySubject.on(.next(.reachable(viaWiFi: reachabilityRef.connection == .wifi)))
}
}
reachabilityRef.whenUnreachable = { _ in
backgroundQueue.async {
reachabilitySubject.on(.next(.unreachable))
}
}
try reachabilityRef.startNotifier()
_reachability = reachabilityRef
_reachabilitySubject = reachabilitySubject
}
deinit {
_reachability.stopNotifier()
}
}
extension ObservableConvertibleType {
func retryOnBecomesReachable(_ valueOnFailure: E, reachabilityService: ReachabilityService) -> Observable<E> {
return self.asObservable()
.catchError { (e) -> Observable<E> in
reachabilityService.reachability
.skip(1)
.filter { $0.reachable }
.flatMap { _ in
Observable.error(e)
}
.startWith(valueOnFailure)
}
.retry()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment