The contradiction of using an unowned reference to resolve a strong reference cycle

Apple's documentation says

define a capture in a closure as an unowned reference when the closure and the instance it captures will always refer to each other, and will always be deallocated at the same time.

Now let me paraphrase that quote in my own words: "In order to use the unowned keyword in a capture list to resolve a strong reference cycle between a closure and a class instance, two conditions must be met. 1)the closure and the instance it captures will always refer to each other. 2)the closure and the instance it captures will always deallocate at the same time."

But this is actually self-contradictory because if the 2nd condition can be met, that is, if the closure and the instance it captures can deallocate at the same time, a strong reference cycle between the closure and the instance wouldn't even exist. By definition, if there's a strong reference cycle between a closure and an instance, neither the closure nor the instance can deallocate. So if the closure and the instance it captures can deallocate at the same time (or can deallocate at all), it means that there's no strong reference cycle between them. And therefore there's no need for a capture list to resolve a strong reference cycle. And that quote from Apple's documentation is basically saying, in order to resolve a strong reference cycle between a closure and an instance by using "unowned" in a capture list, one of the conditions that has to be met is that there's no strong reference cycle between the closure and the instance. That's like saying, there's a solution to a problem, and in order to use that solution to solve the problem, one of the things you have to make sure is that you don't have that problem that the solution is designed to solve. Which makes absolutely no sense.

My question is, how should I interpret and understand this seemingly self-contradictory quote by Apple? Am I misinterpreting the quote? or did Apple make a mistake when writing the documentation?

Sorry, I know my question sounds extremely confusing but I hope you can understand what I'm asking here. Thanks in advance.

Read it like this:

... and, now that the retain-cycle is broken, will always be deallocated at the same time.

The idea is, the closure MUST NOT outlive the object it captures unowned. Otherwise you would crash when executing the closure after the object it captured got deallocated.

Terms of Service

Privacy Policy

Cookie Policy