Skip to content

Commit ada8b5f

Browse files
phlippiebkzaher
authored andcommitted
Fix CLLocationManager extension example
1 parent 8735a75 commit ada8b5f

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

RxExample/Extensions/CLLocationManager+Rx.swift

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,24 @@ extension Reactive where Base: CLLocationManager {
2929
Reactive wrapper for `delegate` message.
3030
*/
3131
public var didUpdateLocations: Observable<[CLLocation]> {
32-
return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:didUpdateLocations:)))
33-
.map { a in
34-
return try castOrThrow([CLLocation].self, a[1])
35-
}
32+
return (delegate as! RxCLLocationManagerDelegateProxy).didUpdateLocationsSubject.asObservable()
3633
}
3734

3835
/**
3936
Reactive wrapper for `delegate` message.
4037
*/
41-
public var didFailWithError: Observable<NSError> {
42-
return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:didFailWithError:)))
43-
.map { a in
44-
return try castOrThrow(NSError.self, a[1])
45-
}
38+
public var didFailWithError: Observable<Error> {
39+
return (delegate as! RxCLLocationManagerDelegateProxy).didFailWithErrorSubject.asObservable()
4640
}
4741

4842
#if os(iOS) || os(macOS)
4943
/**
5044
Reactive wrapper for `delegate` message.
5145
*/
52-
public var didFinishDeferredUpdatesWithError: Observable<NSError?> {
46+
public var didFinishDeferredUpdatesWithError: Observable<Error?> {
5347
return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:didFinishDeferredUpdatesWithError:)))
5448
.map { a in
55-
return try castOptionalOrThrow(NSError.self, a[1])
49+
return try castOptionalOrThrow(Error.self, a[1])
5650
}
5751
}
5852
#endif
@@ -118,7 +112,7 @@ extension Reactive where Base: CLLocationManager {
118112
#endif
119113

120114
#if os(iOS) || os(macOS)
121-
115+
122116
/**
123117
Reactive wrapper for `delegate` message.
124118
*/
@@ -136,11 +130,11 @@ extension Reactive where Base: CLLocationManager {
136130
/**
137131
Reactive wrapper for `delegate` message.
138132
*/
139-
public var monitoringDidFailForRegionWithError: Observable<(region: CLRegion?, error: NSError)> {
133+
public var monitoringDidFailForRegionWithError: Observable<(region: CLRegion?, error: Error)> {
140134
return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:monitoringDidFailFor:withError:)))
141135
.map { a in
142136
let region = try castOptionalOrThrow(CLRegion.self, a[1])
143-
let error = try castOrThrow(NSError.self, a[2])
137+
let error = try castOrThrow(Error.self, a[2])
144138
return (region: region, error: error)
145139
}
146140
}
@@ -176,11 +170,11 @@ extension Reactive where Base: CLLocationManager {
176170
/**
177171
Reactive wrapper for `delegate` message.
178172
*/
179-
public var rangingBeaconsDidFailForRegionWithError: Observable<(region: CLBeaconRegion, error: NSError)> {
173+
public var rangingBeaconsDidFailForRegionWithError: Observable<(region: CLBeaconRegion, error: Error)> {
180174
return delegate.methodInvoked(#selector(CLLocationManagerDelegate.locationManager(_:rangingBeaconsDidFailFor:withError:)))
181175
.map { a in
182176
let region = try castOrThrow(CLBeaconRegion.self, a[1])
183-
let error = try castOrThrow(NSError.self, a[2])
177+
let error = try castOrThrow(Error.self, a[2])
184178
return (region: region, error: error)
185179
}
186180
}
@@ -212,9 +206,6 @@ extension Reactive where Base: CLLocationManager {
212206
return CLAuthorizationStatus(rawValue: Int32(number.intValue)) ?? .notDetermined
213207
}
214208
}
215-
216-
217-
218209
}
219210

220211

RxExample/Extensions/RxCLLocationManagerDelegateProxy.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ import CoreLocation
1515
class RxCLLocationManagerDelegateProxy : DelegateProxy
1616
, CLLocationManagerDelegate
1717
, DelegateProxyType {
18-
18+
19+
internal lazy var didUpdateLocationsSubject = PublishSubject<[CLLocation]>()
20+
internal lazy var didFailWithErrorSubject = PublishSubject<Error>()
21+
1922
class func currentDelegateFor(_ object: AnyObject) -> AnyObject? {
2023
let locationManager: CLLocationManager = object as! CLLocationManager
2124
return locationManager.delegate
2225
}
23-
26+
2427
class func setCurrentDelegate(_ delegate: AnyObject?, toObject object: AnyObject) {
2528
let locationManager: CLLocationManager = object as! CLLocationManager
26-
locationManager.delegate = delegate as? CLLocationManagerDelegate
29+
if let delegate = delegate {
30+
locationManager.delegate = (delegate as! CLLocationManagerDelegate)
31+
} else {
32+
locationManager.delegate = nil
33+
}
34+
}
35+
36+
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
37+
didUpdateLocationsSubject.onNext(locations)
38+
}
39+
40+
public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
41+
didFailWithErrorSubject.onNext(error)
2742
}
2843
}

0 commit comments

Comments
 (0)