我在这部分代码上有一些崩溃:
SRNetwork.provider
.request(SRService.postData(user_id: userId))
.mapArray(STrain.self)
.observeOn(ConcurrentDispatchQueueScheduler.init(queue: SDispatchQueue.dataTrain.getQueue()))
.subscribe({ (event) -> Void in
switch event {
case .next(let response):
self.train.value = response
SRealmTrain.sharedInstance.cacheTrain(response)
case .error(let error):
SRealmTrain.sharedInstance.fetchTrainRx(userId) //CRASH IS HERE
.bindTo(self.train)
.addDisposableTo(self.disposeBag)
print("\(error)")
default: break;
}
})
.addDisposableTo(disposeBag);
我认为问题在于我不在MainScheduler.instance
rxRealm 上,main thread
但我不想要它。有可能修复它吗?
fetchTrainRx:
public func fetchTrainRx(_ userId: String) -> Observable<[STrain]> {
let predicate = NSPredicate(format: "userId == %@", userId)
if let realm = realm {
return Observable.from(realm
.objects(SRTrain.self)
.filter(predicate)
.sorted(byProperty: "order", ascending: true))
.map ({
$0.map(STrain.init)
})
}
return Observable.just([]);
}