나는 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
문서에서
고마워요. 처음 Observable에 가입 할 때마다 현재 상태가 두 번 나에게 주어졌습니다. 제거 시작했습니다. – edulpn
이것은 작동하지만'.distinctUntilChanged()'ater map을 boolean으로 사용할 수 있습니다. 부울은 상황이 다르면 호출을 복제하지 않고 다시 실행합니다. –