RxSwift和CLLocation:尝试获取用户位置时崩溃

时间:2016-04-22 16:51:17

标签: ios swift multithreading cllocationmanager rx-swift

我在我的应用中使用RSwift库。我试图获取用户位置,以便在网络请求中发送它。要获取此位置,我需要使用Observables,因为如果用户没有授权位置,该函数必须抛出错误。

此算法是在主线程之外的另一个线程中运行的许多Observable的连接数组的一部分(为了不冻结UI)。我需要在主线程中执行“获取用户位置”功能,因为如果没有在主线程中执行它崩溃并且崩溃日志是:

fatal error: Executing on backgound thread. Please use `MainScheduler.instance.schedule` to schedule work on main thread

在上面的代码中,有一个geolocation helper init(这是一个单例),并执行该方法来获取用户位置(或错误)。

private var authorized: Observable<Bool?>
private var location: Observable<CLLocationCoordinate2D>
private let locationManager = CLLocationManager()

private init() {
    locationManager.desiredAccuracy = LocationOptions.desiredAccuracy

    authorized = Observable.just(false)
        .map({ _ in CLLocationManager.authorizationStatus() })
        .concat(locationManager.rx_didChangeAuthorizationStatus)
        .distinctUntilChanged()
        .map({ authorizedStatus in
            switch authorizedStatus {
            case .AuthorizedAlways, .AuthorizedWhenInUse:
                return true
            case .NotDetermined:
                return nil
            case .Restricted, .Denied:
                return false
            }
        })
        .catchErrorJustReturn(false)

    location = locationManager.rx_didUpdateLocations
        .filter { $0.count > 0 }
        .map({ return $0.last!.coordinate })
}


func getLocation() -> Observable<Location> {
    return authorized
        .flatMap({ authorized -> Observable<CLLocationCoordinate2D> in
            guard let authorized = authorized else {
                self.locationManager.requestAlwaysAuthorization()
                return Observable.empty()
            }

            if authorized {
                self.startUpdating()
                return self.location
            } else {
                return Observable.error(
                    NSError(
                        domain:"",
                        code: 0,
                        userInfo:nil )
                )
            }
        })
        // We just need one position updated
        .take(1)
        .map({ coordinate in
            self.stopUpdating()

            let location = Location(longitude: coordinate.longitude, latitude: coordinate.latitude, latestUpdatedDate: NSDate())
            if location.isValid() {
                saveLocation(location)
            }
            return location
        })
        .doOnError({ error in self.stopUpdating() })
}

我在RXSwift地理定位示例(https://github.com/ReactiveX/RxSwift/pull/429)中看到了一个可以使用Drivers处理它的类,但我确实需要出错并且Drivers无法返回错误。

如果有人可以帮我解决这个问题,我将不胜感激。

我已经尝试将“.observeOn(MainScheduler.instance)”添加到2个observable中,但它冻结了UI ..也许有更好的方法可以在不冻结UI的情况下执行此操作。

由于

2 个答案:

答案 0 :(得分:0)

@romroll,DelegateProxy目前仅适用于RxCocoa中的主线程。 尝试做这样的事情

.map({ _ in CLLocationManager.authorizationStatus() })
        .observeOn(MainScheduler.instance)
        .concat(locationManager.rx_didChangeAuthorizationStatus)
        .observeOn(someOtherScheduler)
        .distinctUntilChanged()

希望,这有帮助。 如果没有,您可以随时通过RxSwiftSlack与我联系,提及@sergdort或其他人:)

答案 1 :(得分:0)

您必须将扩展CLLocationManager + Rx.swift和RxCLLocationManagerDelegateProxy.swift复制到您的项目,因为它不再是RxCocoa的一部分

相关问题