Retain cycle in SwiftUI view struct

The speaker mentions there is "less risk of creating a retain cycle in when capturing self in a closure within a value type", such as a SwiftUI view struct.

Can you elaborate on when this could actually occur?

Thanks!
Answered by Developer Tools Engineer in 615145022
You can still create retain cycles indirectly. For example, suppose you write something like:

Code Block swift
class MyObject {
var function = { () }
}
struct MyStruct {
let obj = MyObject()
func structMethod() { ... }
init() {
obj.function = { structMethod() }
}
}

The closure will capture self, which retains obj, which retains the closure, so this forms a retain cycle. Most examples like this are pretty contrived, which is why we decided to not require self. before bar() in that closure, but they can happen.
Accepted Answer
You can still create retain cycles indirectly. For example, suppose you write something like:

Code Block swift
class MyObject {
var function = { () }
}
struct MyStruct {
let obj = MyObject()
func structMethod() { ... }
init() {
obj.function = { structMethod() }
}
}

The closure will capture self, which retains obj, which retains the closure, so this forms a retain cycle. Most examples like this are pretty contrived, which is why we decided to not require self. before bar() in that closure, but they can happen.
One follow up...

Above, how would you break / avoid the cycle? You can't create a weak reference to MyStruct in the closure because it is not a reference type. Might just be don't get yourself in this situation :)

Thanks



I was wondering about this, too, and would also be interested in how this could, in theory, be solved. I say "in theory", because in practice it is probably a bad idea to have reference types as properties inside value types in the first place.

My guess is that there's really no way as a struct does not provide a deinit method, is that right?

That being said, does the example as given compile in Swift 5.3? I didn't have the chance to set up a test environment for me yet (sorry), but under 5.2 (where I use an explicit self. of course) this throws a

Escaping closure captures mutating 'self' parameter

for me anyway. I'm having a hard time constructing an example for such a retain cycle on the top of my head, what am I overlooking? Hm...
Retain cycle in SwiftUI view struct
 
 
Q