I'm trying to call the refresh function in the view model from the .refreshable in a view.
I have tested it with an empty function in the viewModel
and it causes a memory leak in this view
struct TopicListView: View {
@StateObject private var viewModel: TopicListViewModel
@State private var boardId: String
@State private var boardName: String
init(dataFetchable: DataFetchable, boardId: String, boardName: String) {
self._viewModel = StateObject(wrappedValue: TopicListViewModel(dataFetchable: dataFetchable))
self.boardId = boardId
self.boardName = boardName
}
var body: some View {
List(viewModel.topics.indices, id: \.self) { index in
HStack {
TopicView(title: viewModel.topics[index].title, lastPostTime: viewModel.topics[index].lastPostTime, formatter: viewModel.dateFormatter)
NavigationLink(destination: PostView(dataFetchable: viewModel.dataFetchable, title: viewModel.topics[index].title, topicId: viewModel.topics[index].id)) {
EmptyView()
}
.frame(width: 0)
.opacity(0)
}
.onAppear {
if index == viewModel.topics.count-1 {
viewModel.page+=1
viewModel.fetchTopics(boardId)
}
}
}
.listStyle(.plain)
.navigationBarTitle(boardName)
.refreshable {
await viewModel.doNotThing()
}
.onAppear {
viewModel.boardId = boardId
}
}
}
However, the PostView
which I have linked with a NavigationLink have no issue
struct PostView: View {
@State private var title: String
@State private var topicId: String
@StateObject private var viewModel: PostViewModel
init(dataFetchable: DataFetchable, title: String, topicId: String) {
self._viewModel = StateObject(wrappedValue: PostViewModel(dataFetchable: dataFetchable))
self.topicId = topicId
self.title = title
}
var body: some View {
List(viewModel.posts, id: \.id) { item in
TextblockView(textBlock: item.textBlock)
}
.listStyle(.plain)
.navigationBarTitle(title)
.navigationBarTitleDisplayMode(.inline)
.refreshable {
await viewModel.doNotThing()
}
.onAppear(perform: {
viewModel.topicId = topicId
})
}
}
Here is the memory graph
Any ideas how to fix it?