gpt4 book ai didi

swift - 显示分页结果,同时观察 Realm Swift 中的变化

转载 作者:行者123 更新时间:2023-11-28 13:32:00 25 4
gpt4 key购买 nike

我有一个典型的消息传递应用程序,其中消息存储为 Realm 对象。我想以观察结果的安全方式在集合/表格 View 中显示对话消息

let results = realm.objects(Message.self).filter(predicate)
// Observe Results Notifications
notificationToken = results.observe { [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()
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()
case .error(let error):
// An error occurred while opening the Realm file on the background worker thread
fatalError("\(error)")
}
}

现在,假设我显示所有消息,这将起作用。因为它们可能很多,所以我需要分页加载。

我怎样才能跟踪变化?

我正在寻找一种方法来获取已更改消息的某种 ID,但我找不到任何东西。

最佳答案

Realm 对象是延迟加载的,因此它们在被访问之前不会“占用空间”。在我们的例子中,结果中有 1000 个对象,但一次只显示 10 个。这些是“占用空间”的 10 个。因此,拥有大型结果数据集可能不是问题。

当您从 Realm 填充结果对象时,每个对象都有一个索引。将结果视为数组。第一个对象是索引 0,第二个对象在索引 1 中,依此类推。

当一个对象在 Realm 中被修改时,该信息被传递给您的观察者,然后您可以更新您的 UI。

假设我们有一个具有人名和电子邮件的 PersonClass Realm 对象

PersonClass: Object {
@objc dynamic var name = ""
@objc dynamic var email = ""
}

我们想要显示一个人员列表,如果电子邮件地址发生变化,我们希望根据该变化更新 UI。

当应用程序启动时,我们将所有人员加载到结果类变量中。

override func viewDidLoad() {
super.viewDidLoad()
self.personResults = realm.objects(PersonClass.self)

然后我们向这些结果添加一个观察者,以便应用程序收到更改通知。 .initial 将在加载结果后运行,因此它是填充数据源和刷新 tableView 的好地方。

func doObserve() {
self.notificationToken = self.personResults!.observe { (changes: RealmCollectionChange) in

switch changes {
case .initial: // initial object load complete
if let results = self.personResults {
for p in results {
print(p.name, p.email) //populate datasource, refresh
}
}
break

case .update(_, let deletions, let insertions, let modifications):
for index in deletions {
print(" deleted object at index: \(index)")
}

for index in insertions {
print(" inserted object at index: \(index)")
}

for index in modifications {
print(" modified object at index: \(index)")
let person = self.personResults![index]
print("modified item: \(person.name) \(person.email)")
}

break

case .error(let error):
fatalError("\(error)")
break

}
}
}

在此示例中,当存储在索引 #2 的人更改他的电子邮件时,观察者会对此做出响应并将姓名和新电子邮件打印到控制台。

但是……

Realm 是实时更新的,如果您刷新 tableView 或什至只是刷新那一行,以便从对象重新加载单元格,UI 将被更新。我不知道“那我怎样才能跟踪变化?”意味着在您的用例中,但您实际上可以删除所有 for 循环并在 .update 部分中只有一个 tableView.reloadData 并且 UI 将随着数据更改而更新。或者,您可以使用索引并只更新该行。

请记住,Results 中的 Realm 对象是实时的,并且随着数据的变化始终保持最新。

另外需要注意的是很多Realm对象都会有一个对象的唯一标识,这样定义

class PersonClass: Object {
@objc dynamic var person_id = UUID().uuidString
override static func primaryKey() -> String? {
return "person_id"
}
}

可用于访问 Realm 中的特定对象,但与您的问题没有直接关系。

关于swift - 显示分页结果,同时观察 Realm Swift 中的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57222967/

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