Hi, folks.
I know that in the new observation, class property changes can be automatically notified to SwiftUI, which is very convenient. But in the new observation framework, how to monitor the property changes of different model classes? For example, class1 has an instance of class2, and I need to notify class1 to perform some actions and make some changes when some properties of class2 are changed. How to do it in observation? In the past, I could use combined methods to write the second part of the code for monitoring. However, using the combined framework in observation is a bit confusing. I know this method can be withObservationTracking(_:onChange:) but it needs to be registered continuously.
If Observation is not possible, do I need to change my design structure?
Thanks.
// Observation
@Observable class Sample1 {
var count: Int = 0
var name = "Sample1"
}
@Observable class Sample2 {
var count: Int = 0
var name = "Sample2"
var sample1: Sample1?
init (sample1 : Sample1) {
self.sample1 = sample1
}
func render() {
withObservationTracking {
print("Accessing Sample1.count: \(sample1?.count ?? 0)")
} onChange: { [weak self] in
print("Sample1.count changed! Re-rendering Sample2.")
self?.handleSample1CountChange()
}
}
private func handleSample1CountChange() {
print("Handling count change in Sample2...")
self.count = sample1?.count ?? 0
}
}
// ObservableObject
class Sample1: ObservableObject {
@Published var count: Int = 0
var name = "Sample1"
}
class Sample2: ObservableObject {
@Published var count: Int = 0
var name = "Sample1"
var sample1: Sample1?
private var cancellables = Set<AnyCancellable>()
init (sample1 : Sample1) {
self.sample1 = sample1
setupSubscribers()
}
private func setupSubscribers() {
sample1?.$count
.receive(on: DispatchQueue.main)
.sink { [weak self] count in
guard let self = self else { return }
// Update key theory data
self.count = count
self.doSomeThing()
}
.store(in: &cancellables)
}
private func doSomeThing() {
print("Count changes, need do some thing")
}
}
Post
Replies
Boosts
Views
Activity
I want to add a tool bar (setting search )to my app just like the apple file app using pure swiftUI, is it possible, if not, can i using a UIKit to implement it.
struct MainView: View {
var body: some View {
TabView {
Tab("View 1", systemImage: "square.grid.3x2") {
View1()
}
Tab("View 2", systemImage: "square.grid.2x2") {
View2()
}
}
.tabViewStyle(.sidebarAdaptable)
}
According to the MVVM design pattern, one of my views depends on many properties in my model. Can I use logic like @published var model = MyModel()? Will there be a large performance loss? Will the UI be refreshed when other unrelated properties in the model are modified? What is the best practice in this case?
As a starter for iOS programming, I want to get the touch’ x-y position on a SwiftUI view , what is the fastest way/api or the highest refresh rate to get the position . UItouch in UIkit ?or other api?