Wondering if I'm doing something obviously wrong in the code below. It very loosely mimics the pattern used to capture Effects in the Swift Composable Architecture. After tapping "Press Me" and waiting for the Text to update, Xcode's "Debug Memory Graph" is showing a leaked Future<String,Never>.Conduit
. Instruments shows it as well. Tapping "Press Me" again and waiting leaks a second Future<String,Never>.Conduit
. Interestingly, if I tap "Press Me" multiple times in quick succession, only the Futures that complete, leak.
The code below was written to drop into "ContentView.swift" in a new "Single View App" project in Xcode. I've replicated this finding with Xcode 11.2.1, 11,3.1, 11.4.1, 11.5beta & 11.5beta2.
I've filed Feedback #7700518
Thanks,
--David
import Combine
import SwiftUI
var futureCancellable: AnyCancellable?
struct ContentView: View {
@State var buttonPressTime: String? = nil
var body: some View {
VStack {
Text(buttonPressTime ?? "Not Pressed Yet")
Button(
action: {
futureCancellable = Future<String,Never> { callback in
let timeString = "\(Date())"
DispatchQueue.main.asyncAfter(
deadline: .now() + 2
) {
callback(.success(timeString))
}
}.sink(
receiveCompletion: { _ in
futureCancellable = nil
},
receiveValue: {
self.buttonPressTime = $0
}
)
},
label: {
Text("Press Me")
}
)
}
}
}