I’ve the follwing SwiftUI view and units
is a state repeatedly getting up to date from dad or mum view. If I navigate to DetailView
by one of many hyperlink and the corresponding system
is faraway from units
whereas I’m viewing its DetailView
, the navigation pops again. How do I forestall it from popping again even when system
is faraway from units
?
class Datasource : ObservableObject{
@Printed public var units : [devices]
}
struct ContentView: View {
@StateObject personal var datasource = Datasource()
var physique: some View {
NavigationView {
DeviceList(units: datasource.units)
.padding()
.navigationTitle("Demo")
.navigationBarTitleDisplayMode(.inline)
}.navigationViewStyle(.stack)
}
}
struct DeviceList: View{
var units: [Device]
var physique: some View{
if(units.isEmpty)
{
Textual content("Scanning...")
}
else
{
ScrollView{
LazyVStack {
ForEach(units) { system in
NavigationLink{
DetailView(system: Gadget)
} label: {
DeviceItem(system: Gadget)
}
.buttonStyle(.plain)
}
}
}
}
}
}
I’ve discovered this publish which talked about an answer to pause the replace of ObservableObject
, which appears fixing my drawback however I do not need to modify my datasource to PausableObservableObject
. As an alternative I wish to create a generic class class Pausable<T> : PausableObservableObject
to encapsulate my datasource, however I do not know the proper strategy to do it. Right here is my try which isn’t efficiently.
class Pausable<T> : PausableObservableObject{
@Printed var worth: T
personal var sink: AnyCancellable?
init(initialValue:T, writer: Printed<T>.Writer) {
self.worth = initialValue
tremendous.init()
self.$worth = writer
sink = writer.sink {
self.worth = $0
self.publishWillUpdate()
}
}
}