RxCoreData bindTo TableView如何在更改模型时更新UI
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var addBarButtonItem: UIBarButtonItem!
var managedObjectContext: NSManagedObjectContext!
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
bindUI()
configureTableView()
}
func bindUI() {
addBarButtonItem.rx.tap
.map { _ in
Event(id: "1", date: Date(timeIntervalSince1970: 0))
/* Change Date(timeIntervalSince1970: 1) */
}.subscribe(onNext: { [weak self] (event) in
_ = try? self?.managedObjectContext.rx.update(event)
})
.addDisposableTo(disposeBag)
}
func configureTableView() {
tableView.isEditing = true
let animatedDataSource = RxTableViewSectionedAnimatedDataSource<AnimatableSectionModel<String, Event>>()
animatedDataSource.configureCell = { dateSource, tableView, indexPath, event in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "\(event.date)"
print("\(event.date)")
return cell
}
managedObjectContext.rx.entities(Event.self, sortDescriptors:[NSSortDescriptor(key: "date", ascending: false)])
.map { events in
[AnimatableSectionModel(model: "Section 1", items: events)]
}
.bindTo(tableView.rx.items(dataSource: animatedDataSource))
.addDisposableTo(disposeBag)
}
}
当
事件(id:“1”,日期:日期(时间间隔1970:0))
转换为事件(id:“1”,日期:日期(TimeIntervalsSince1970:1))
最佳答案
此链接中问题的答案
https://github.com/ReactiveX/RxSwift/issues/1115
关于ios - RxCoreData UI未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41254727/