2017-01-24 6 views
0

나는 CLLocationManager을 처리해야하는 CoreLocation 관리자가 있으며 RxSwift (및 Extensions 및 DelegateProxies)를 통해 관찰 가능한 속성을 제공합니다. LocationRepository은 다음과 같습니다RxSwift 드라이버 처음으로 두 번 호출하는 중

class LocationRepository { 
    static let sharedInstance = LocationRepository() 
    var locationManager: CLLocationManager = CLLocationManager() 
    private (set) var supportsRequiredLocationServices: Driver<Bool> 
    private (set) var location: Driver<CLLocationCoordinate2D> 
    private (set) var authorized: Driver<Bool> 

    private init() { 
     locationManager.distanceFilter = kCLDistanceFilterNone 
     locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation 

     supportsRequiredLocationServices = Observable.deferred { 
      let support = CLLocationManager.locationServicesEnabled() && CLLocationManager.significantLocationChangeMonitoringAvailable() && CLLocationManager.isMonitoringAvailable(for:CLCircularRegion.self) 
      return Observable.just(support) 
     } 
     .asDriver(onErrorJustReturn: false) 

     authorized = Observable.deferred { [weak locationManager] in 
      let status = CLLocationManager.authorizationStatus() 
      guard let locationManager = locationManager else { 
       return Observable.just(status) 
      } 
      return locationManager.rx.didChangeAuthorizationStatus.startWith(status) 
     } 
     .asDriver(onErrorJustReturn: CLAuthorizationStatus.notDetermined) 
     .map { 
      switch $0 { 
      case .authorizedAlways: 
       return true 
      default: 
       return false 
      } 
     } 

     location = locationManager.rx.didUpdateLocations.asDriver(onErrorJustReturn: []).flatMap { 
      return $0.last.map(Driver.just) ?? Driver.empty() 
     } 
     .map { $0.coordinate } 
    } 

    func requestLocationPermission() { 
     locationManager.requestAlwaysAuthorization() 
    } 
} 

내 발표자는 다음 저장소 속성에 대한 변경을 대기. 그것은 작업을 수행

class LocatorPresenter: LocatorPresenterProtocol { 
    weak var view: LocatorViewProtocol? 
    var repository: LocationRepository? 
    let disposeBag = DisposeBag() 

    func handleLocationAccessPermission() { 
     guard repository != nil, view != nil else { 
      return 
     } 

     repository?.authorized.drive(onNext: {[weak self] (authorized) in 
      if !authorized { 
       print("not authorized") 
       if let sourceView = self?.view! as? UIViewController, let authorizationView = R.storyboard.locator.locationAccessRequestView() { 
        sourceView.navigationController?.present(authorizationView, animated: true) 
       } 
      } else { 
       print("authorized") 
      } 
     }).addDisposableTo(disposeBag) 
    } 
} 

,하지만 난 인증 상태를 확인하려고 처음으로 두 번 호출 Driver 받고 있어요, 그래서 액세스 요청보기는 두 번되게됩니다 : LocatorPresenter은 다음과 같습니다. 내가 여기서 무엇을 놓치고 있니?

감사합니다. startWith 문서에서

답변

1

:

StartWith 내가 그것을 시도하지 않은

관찰 가능한 소스에서 항목을 방출하기 시작하기 전에 항목 지정된 순서를 방출하지만, 아마도 경우 startWith(status)을 제거하면 상태가 두 번 표시되지 않습니다. ------------------------------

을 :

그것은 당신이 관찰에서 다음 순서를 받고있는 것 같다 --- 무단 ----> ----- 공인 그래서 라인

:

startWith(status) // status is unauthorized 

당신이 마침내 하나 얻을 :

를 ------- unauthorized- -------- 무단 ----인가 ----->

+0

고마워요. 처음 Observable에 가입 ​​할 때마다 현재 상태가 두 번 나에게 주어졌습니다. 제거 시작했습니다. – edulpn

+0

이것은 작동하지만'.distinctUntilChanged()'ater map을 boolean으로 사용할 수 있습니다. 부울은 상황이 다르면 호출을 복제하지 않고 다시 실행합니다. –