[email protected]
Update question
let sortProperties = [SortDescriptor
(keyPath: “isAnswered”, ascending: true),
SortDescriptor(keyPath: "isFavorite",
ascending: false),
SortDescriptor(keyPath: “voteCount",
ascending: false)]
questions = realm?.objects(Question.self)
.filter("status = true”)
.sorted(by: sortProperties)
notificationToken =
questions.addNotificationBlock { [weak self]
(changes: RealmCollectionChange) in
guard let tableView = self?.questTableView
else { return }
switch changes {
case .initial:
DispatchQueue.main.async {
tableView.reloadData()
}
break
// case .update
}
}
case .update(_, let deletions, let insertions,
let modifications):
DispatchQueue.main.async {
tableView.beginUpdates()
tableView.insertRows(at:
insertions.map({ IndexPath(row: $0,
section: 0) }), with: .automatic)
tableView.deleteRows(at: deletions.map({
IndexPath(row: $0, section: 0)}),
with: .automatic)
tableView.reloadRows(at:
modifications.map({ IndexPath(row: $0,
section: 0) }), with: .automatic)
tableView.endUpdates()
}
break
//