Search code examples
uitableviewrealm

rxCocoa Binding error to UI: objectDeleted


I have a list of items store in Realm. Use rxSwift to bind items to UITableview, and it runs fine. When I delete one item from Realm, it run into this error "Binding error to UI: objectDeleted" In the tableviewcell have this code

    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
    }
}

the isReaded Observable is like this

    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
}()

Has any clue what's wrong of my code. I'm new to rxswift.


Solution

  • I find out It's the changeObservable that emit an error when an object is deleted from the realm. Fix the error like this:

        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
    }()
    

    catch an error and return a default value.