- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的应用程序中使用 RSwift 库。我正在尝试获取用户位置以便在网络请求中发送它。要获取此位置,我需要使用 Observables,因为如果用户未授权位置,该函数必须抛出错误。
该算法是许多 Observable 的连接数组的一部分,这些 Observable 在主线程之外的另一个线程中运行(为了不卡住 UI)。我需要在主线程中执行“获取用户位置”功能,因为如果不在主线程中执行它会崩溃并且崩溃日志是:
fatal error: Executing on backgound thread. Please use `MainScheduler.instance.schedule` to schedule work on main thread
在上面的代码中,有地理定位助手初始化(这是一个单例)和执行获取用户位置(或错误)的方法。
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 个可观察对象,但它卡住了 UI。也许有更好的方法可以在不卡住 UI 的情况下执行此操作。
谢谢
最佳答案
@romroll,DelegateProxy
目前仅在 RxCocoa 的主线程上工作。尝试做这样的事情
.map({ _ in CLLocationManager.authorizationStatus() })
.observeOn(MainScheduler.instance)
.concat(locationManager.rx_didChangeAuthorizationStatus)
.observeOn(someOtherScheduler)
.distinctUntilChanged()
希望这对您有所帮助。如果没有,您可以随时通过 RxSwiftSlack 联系我,通过提及@sergdort 或其他人 :)
关于ios - RxSwift 和 CLLocation : crash when trying to get user location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36799380/
我尝试使用 RxSwift 作为依赖项创建一个名为 MyLib 的库,它使用 cocoapod 命令 pod lib create。但是,以下代码不起作用。 import UIKit import M
我是 RxSwift 的新手。很新。通常要在没有 Rx 的情况下链接你,你将下一个函数放在一个完成闭包中。我的同事告诉我改用 flatmap 或我们的 concatmap 版本,但从未给我一个例子。有
我创建了一个使用 RxSwift、RxCocoa 和其他第三方框架的自定义框架。自定义框架正确编译,但当我添加到应用程序时,它崩溃并出现以下错误: dyld: Library not loaded:
我会尽可能简短:我们启动了完整的 RxSwift 应用程序,但我们的协调员开始不知所措,难以维护和扩展,因此我们正在重新设计他们以不使用 RxSwift,现在我们不知道如何使用它们以及保存它们的位置。
我正在使用 RxSwift 和 MVVM 实现一个简单的地址 UITableView。我像这样在我的 View Controller 中创建了一个绑定(bind)。 viewModel.address
ReactiveX.io documentation使用 AnonymousDisposable,它在 RxSwift 2.x 中,但在 RxSwift 3.0 中是一个未解析的标识符。我应该改用什么
我有一个应用程序使用 MVP与 Coordinator图案。 当子协调器发送事件时,我希望我的 AppCoordinator递归调用一个方法,根据一些 SessionState 选择下一个协调器. 该
上下文:我的团队正在开始一个新的中型 Swift 项目(大约 20 MM),我正在考虑在 RxSwift 中开发它.我的一位经理怀疑,他曾经在 上的调试方面有过不好的经历。响应式(Reactive)编
嘿,我在 HackingWithSwift 教程中学到了如何使用 Combine 发出链接的网络请求(请参阅下面的代码)。现在我将使用 RXSwift 构建相同的逻辑,但我不知道如何像在 Combin
我正在尝试进行几个 API 调用并填充领域数据库。 一切正常。但是,当我尝试运行 performSegue()在 subscribe()方法引发异常,通知我不能在后台线程上执行此操作,这是完全合理的。
假设我有 Int 数组: var items = [1, 2, 3, 4, 5] 以及一个接受 Int 参数并基于它发送网络请求的函数: func sendRequest(argument: Int
我有一个文本字段,它有 2 个验证规则:最小数量的字符和字母数字字符。 我希望能够向用户展示他在错误标签中做错了什么,但问题是,如果我将文本字段绑定(bind)到两个规则,它可能会令人毛骨悚然,因为一
我试图在选择表行时在后台执行复杂的计算,但它卡住了 UI。请查看我的代码并告诉我可能有什么问题。 tableView.rx .modelSelected(Sring.self) .fl
我正在尝试构建一个计时器,该计时器从15秒开始,递减计数到0。 事情是,我想根据事件将计时器更新2秒。 到目前为止,这是我尝试做的事情: struct ViewModel { struct I
我正在使用该领域作为后端。使用RxSwift,我可以在DB表中观察到某些列(它跟踪未决状态)。得到事件后,我切换到另一个Serial计划表,以将列的值设置为flatMap块中的Progress状态(返
我对响应式(Reactive)编程很陌生,所以我仍然很难理解它是如何工作的。 我正在尝试做的事情:当用户点击 signUpButton (signUpTrigger) 时,会触发多个 observab
以下是与问题39类似的示例:http://reactivex.io/learnrx/ 我正在尝试将方法调用search(query: String)转换为这些调用的序列。 他们通过创建一个Variab
我有两个 Single 值,它们包含同一对象的数组。我想合并它们并应用 map 并将它们作为单个返回。可能吗? 所以我有这个: func fetchTripList(type: TripType) -
使用 RxSwift,假设我有一个包含整数可观察值的 A 类 class A: { let count: Observable } 以及 A 的可观察对象集合 let data: Observ
我想转换一个我已经完全接收的 ViewModel。该应用程序计算算术表达式。 ViewModel 有两个字符串,一个带有“逻辑”符号,将发送到表达式求值器以获取结果,另一个带有其他符号的“演示”字符串
我是一名优秀的程序员,十分优秀!