I have tabbed app and I know views get destroyed every time the user goes to a different tab and navigates back. However in on of my tabs I have a scroll view that will contain a bunch of content. I dont want to reload the content every time the user switches tabs and comes back. Is there anyway to save the content in the scroll view so it is automatically displayed once the user comes back to the tab. If there is no way to do this do I have to reload the content of the scroll view everytime?
viewModel:
import Foundation
class FeedViewModel: ObservableObject{
@Published var content = [Feed]()
var conversations: [Feed] {
return content
}
view:
struct JobsView: View {
@ObservedObject var viewModel = FeedViewModel()
ZStack(alignment: .bottomTrailing){
ScrollView{
LazyVStack{
ForEach(viewModel.conversations){ content in
FeedRowView(content: content)
.padding()
}
}
}
}
CodePudding user response:
Change
@ObservedObject var viewModel = FeedViewModel()
To
@StateObject var viewModel = FeedViewModel()
StateObject
is for initializing and ObservedObject
is for observing changes on an object whose lifecycle is being managed some other way.