I’ve the follwing SwiftUI view and gadgets
is a state repeatedly getting up to date from mother or father view. If I navigate to DetailView
by one of many hyperlink and the corresponding system
is faraway from gadgets
whereas I’m viewing its DetailView
, the navigation pops again. How do I forestall it from popping again even when system
is faraway from gadgets
?
class Datasource : ObservableObject{
@Printed public var gadgets : [devices]
}
struct ContentView: View {
@StateObject personal var datasource = Datasource()
var physique: some View {
NavigationView {
DeviceList(gadgets: datasource.gadgets)
.padding()
.navigationTitle("Demo")
.navigationBarTitleDisplayMode(.inline)
}.navigationViewStyle(.stack)
}
}
struct DeviceList: View{
var gadgets: [Device]
var physique: some View{
if(gadgets.isEmpty)
{
Textual content("Scanning...")
}
else
{
ScrollView{
LazyVStack {
ForEach(gadgets) { system in
NavigationLink{
DetailView(system: Machine)
} label: {
DeviceItem(system: Machine)
}
.buttonStyle(.plain)
}
}
}
}
}
}
I’ve discovered this submit which talked about an answer to pause the replace of ObservableObject
, which appears fixing my downside however I do not wish to modify my datasource to PausableObservableObject
. As a substitute I want to create a generic class class Pausable<T> : PausableObservableObject
to encapsulate my datasource, however I do not know the right technique 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()
}
}
}