我在Realm中有一个商品列表。 使用rxSwift将项绑定到UITableview,它运行正常。当我删除 来自Realm的一个项目,它遇到了这个错误"绑定错误到UI:objectDeleted" 在tableviewcell中有这段代码
var model: ServerMailCellModel? {
didSet {
subjectLabel.text = model?.subject
timeLabel.text = model?.timeString
fromLabel.text = model?.from
// this one make the **fatal error of bind to UI: objectdeleted**
model?.isReaded.bind(to: statusDotView.rx.isHidden).addDisposableTo(bag)
scrollView.datasource = model!.attachments
}
}
isReaded Observable就像这样
lazy var isReaded: Observable<Bool> = {
// mail is an realm object
guard let mail = StoreManager.mail(with: mailID) else {return Observable.empty()}
// change is a observable that contains realm object property change info
let change = StoreManager.mailChange(with: mailID)
let o: Observable<Bool> = change
.filter({
let name: String = $0.name
return (name == "flags")
})
.map{ property in
let v = property.newValue! as! Int
return ((v & MCOMessageFlag.seen.rawValue) != 0)
}.startWith(mail.isReaded)
return o
}()
有什么迹象表明我的代码有什么问题。我是rxswift的新手。
答案 0 :(得分:0)
我发现当从域中删除对象时,changeObservable会发出错误。像这样修复错误:
lazy var isReaded: Driver<Bool> = {
guard let mail = StoreManager.mail(with: mailID) else {return Observable.empty().asDriver(onErrorJustReturn: false)}
let change = StoreManager.mailChange(with: mailID)
let o: Driver<Bool> = change
.filter({
let name: String = $0.name
return (name == "flags")
})
.map{ property in
let v = property.newValue! as! Int
return ((v & MCOMessageFlag.seen.rawValue) != 0)
}.startWith(mail.isReaded).asDriver(onErrorJustReturn: false)
return o
}()
捕获错误并返回默认值。