gpt4 book ai didi

ios - 如果结果在通知初始化后更新,我该如何使用 Realm 通知?

转载 作者:搜寻专家 更新时间:2023-11-01 05:56:30 24 4
gpt4 key购买 nike

我使用带有 Realm 结果的 UITableView 作为数据源。结果通过 Realm 通知注册,并在发生更改时得到很好的更新。但是,同一个 TableView 有一个搜索栏,可以根据用户的搜索查询过滤结果。这也可以正常工作,但如果通知监听器识别出更新,则部分和行数不匹配。这样做的正确方法是什么?是否可以更新 NotificationToken?

这是初始化通知 token 的代码:

let realm = try! Realm()

results = realm.objects(Person.self).filter("active = '1'").sorted(byProperty: "name", ascending: true)

// Observe Results Notifications
notificationToken = results?.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in

guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
// Results are now populated and can be accessed without blocking the UI
tableView.reloadData()
break
case .update(_, let deletions, let insertions, let modifications):

// Query results have changed, so apply them to the UITableView
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
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
print("Error: \(error)")
break
}
}

此代码根据用户搜索输入更新结果:

func updateSearchResults(for searchController: UISearchController) {

let searchText = searchController.searchBar.text!

if searchText.isEmpty == false {

results = results?.realm?.objects(Person.self).filter("active = '1'")

results = results?.filter("name BEGINSWITH[c] %@ OR lastName CONTAINS[c] %@", searchText, searchText)

results = results?.sorted(byProperty: "name", ascending: true)

self.tableView.reloadData()

}
else {

results = results?.realm?.objects(Person.self).filter("active = '1'").sorted(byProperty: "name", ascending: true)

self.tableView.reloadData()
}
}

如果执行了搜索 qwuery 并且之前更新了 Realm 数据库,则会发生 NSRange 异常。

最佳答案

您在 updateSearchResults() 中覆盖了 results。通知和 notificationToken 绑定(bind)到特定的 Results 对象和查询。因此,如果您用另一个查询覆盖了 results,您应该停止之前的通知,然后将通知重新添加到新的 results 对象。

所以 updateSearchResults() 方法应该像下面这样:

func updateSearchResults(for searchController: UISearchController) {
let searchText = searchController.searchBar.text!
if searchText.isEmpty == false {
results = results?.realm?.objects(Person.self).filter("active = '1'")
results = results?.filter("name BEGINSWITH[c] %@ OR lastName CONTAINS[c] %@", searchText, searchText)
results = results?.sorted(byProperty: "name", ascending: true)
}
else {
results = results?.realm?.objects(Person.self).filter("active = '1'").sorted(byProperty: "name", ascending: true)
}

notificationToken.stop()
notificationToken = results?.addNotificationBlock { [weak self] (changes: RealmCollectionChange) in
guard let tableView = self?.tableView else { return }
switch changes {
case .initial:
tableView.reloadData()
break
case .update(_, let deletions, let insertions, let modifications):
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
case .error(let error):
print("Error: \(error)")
break
}
}
}

关于ios - 如果结果在通知初始化后更新,我该如何使用 Realm 通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41142827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com