We are seeing a memory leak when using a combine TimePublisher (as in the code below) to trigger a fade out of a view.
This is pretty simplified, but I'm struggling to see where we are going wrong. Removing the onAppear block or changing it to set showRedView to false directly removes the leak.
It seems directly tied to our use of the publisher.
We are seeing this leak in both Xcode 11 and Xcode 12, so doesn't seem related to the current beta.
Any help is appreciated as always, thanks!
This is pretty simplified, but I'm struggling to see where we are going wrong. Removing the onAppear block or changing it to set showRedView to false directly removes the leak.
It seems directly tied to our use of the publisher.
Code Block class ContentViewModel: ObservableObject { @Published var showRedView = true var subscriptions: Set<AnyCancellable> = [] func fadeRedView() { Timer.publish(every: 5.0, on: .main, in: .default) .autoconnect() .prefix(1) .sink { [weak self] _ in withAnimation { self?.showRedView = false } } .store(in: &subscriptions) } } struct ContentView: View { @ObservedObject var viewModel = ContentViewModel() var body: some View { ZStack { if viewModel.showRedView { Color.red .transition(.opacity) } Text("Hello, world!") .padding() } .onAppear { self.viewModel.fadeRedView() } } }
We are seeing this leak in both Xcode 11 and Xcode 12, so doesn't seem related to the current beta.
Any help is appreciated as always, thanks!