class Buffer { let data: Data init(data: Data) { self.data = data } deinit { print("deinit buffer", data) } } actor ViewModel: ObservableObject { var currentTask: Task<Buffer, Never>? func action(_ new: @escaping () async -> Data) { let old = currentTask currentTask = Task { _ = await old?.value let data = await new() print("allocated", data) return Buffer(data: data) } } }
Buffer
はひとつ遅れて開放されるので若干リークといえるかもしれませんが…