Observers #430
Replies: 2 comments 1 reply
-
|
Hi @ryangittings, can you explain a bit more how you plan to use this tool? There are a few ways to do this with the library. First, the property wrappers like struct FeatureView: View {
@FetchAll var reminders: [Reminder]
var body: some View {
// Changes to 'reminders' will be observed by the view
ForEach(reminders) { … }
}
}This works with @Observable class FeatureModel {
@ObservableIgnored // This is needed due to a quirk of @Observable, but observation still works
@FetchAll var reminders: [Reminder]
}
struct FeatureView: View {
let model: FeatureModel
var body: some View {
// Changes to 'model.reminders' will be observed by the view
ForEach(model.reminders) { … }
}
}Further, each property wrapper has a projected value that can give you a publisher: class FeatureViewController: UIViewController {
@FetchAll var reminders: [Reminder]
func viewDidLoad() {
$reminders.publisher
.sink { … }
.store(in: &cancellables)
}
}And if you want to depend on our Swift Navigation library, then there is a general purpose class FeatureViewController: UIViewController {
@FetchAll var reminders: [Reminder]
func viewDidLoad() {
observe { [weak self] in
guard let self else { return }
// The act of accessing 'reminders' causes its changes to be observed.
reloadDataSource(reminders)
}
}
}We have a case study for this latter style that shows how to power a sqlite-data/Examples/CaseStudies/UIKitDemo.swift Lines 76 to 82 in 0eed823 |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the reply! I currently don't use it for visual reasons - it's for syncing local state to remote basically. I don't think any of the examples above are ideal? It's so easy with Realm , e.g.: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It'd be great to be able to observe changes to (similar to
token = await items.observe(on: self) { [weak self] _, changes inin realm?Beta Was this translation helpful? Give feedback.
All reactions