Michael Long
Jun 21, 2024

--

Unfortunately, the initial precept is wrong. If the sink captures self, then you have a strong reference cycle and the app won't crash when the object is "destroyed"... since it can't be destroyed.

The view could release the view model, but the strong cycle will ensure that it persists. Weak self fixes the problem but it is indeed troublesome if you forget... and cause a memory leak.

That's why moving past Combine into async/await is preferable as it avoids pesky closure captures.

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

Responses (1)