What is the use of unjustified links?

Weak and unpublished links are used to prevent save cycles in a situation where two objects contain a link to another. I use weak, but I do not use unowned. Here is an example of Apple in a situation where one of the two objects should use an unpublished link:

class Customer { let name: String var card: CreditCard? init(name: String) { self.name = name } } class CreditCard { let number: UInt64 unowned let customer: Customer init(number: UInt64, customer: Customer) { self.number = number self.customer = customer } } 

The idea is that a credit card cannot exist without a customer. Therefore, a credit card can do without additional sweep, which entails the use of weak links, and instead can use an unpublished link. Hmmm ... so why not use a strong link? If all other links to the client were supposed to go away (what should not happen?), Then using a credit card from the certificate belonging to them would lead to collapse; while using a strong link will result in a memory leak. A? The choice between two evils? Is it better to crash because it will most likely be noticed during development and testing?

Please help with some understanding. Thanks.

+6
source share
6 answers

This is actually not a problem, because as it stands, the unowned link does not create any strong reference loop. When a Customer object is released, its CreditCard will also be immediately released. Your CreditCard will never be able to refer to a freed Customer .

0
source

Better crash because it will most likely be noticed during development and testing?

Yes.

Well, not quite.

The idea is that your application design must ensure that the CreditCard instance CreditCard not match that Customer instance. When you use unowned , you trust yourself to have a design in the game that logically guarantees performance without failures.

Now, why has anyone ever used unowned over weak ? Just! unowned removes all the complexity of Optional deployment, and if you know that your CreditCard instance CreditCard never survive its corresponding Customer instance, then you should use unowned every way.

+3
source

unowned is actually much better than weak in those situations where it is appropriate (i.e. sure that the undeserved object will not disappear), because:

  • A weak link must be optional, which may need to be expanded, and

  • A weak link entails large amounts of overhead to track the link and change it to nil if it is canceled, whereas an unused link entails overhead.

+3
source

Very interesting question. Here is some difference between weak and unsettled links according to Apple documentation .

Weak links

A weak link is a link that does not hold strong binding to the instance to which it refers, and therefore does not stop ARC from disposing of the reference instance. This behavior prevents the link from becoming part of a strong reference loop.

Unpublished Links

Like a weak link, an unpublished link does not hold strong control over the instance to which it refers. However, unlike a weak link, a non-primary link is used when another instance has the same lifetime or longer life.

The answer to your question:

weak can become zero, while unowned is considered to never become nil, so weak will be optional if unowned optional.

In this case, Customer may or may not have a CreditCard , but without a CreditCard must exist without a Customer .

0
source

Ok, I finally realized:

  • The last customer link (except credit card) is set to zero.
  • ARC checks the client reference count:
  • Credit card has a strong link. The reference count will be 1, so ARC will not free it. The result is a memory leak.
  • or, a credit card has an unpublished link. The reference count will be 0, so ARC will free it. This will cause the credit card counter to go to 0, which will lead to its release. Consequently, the Credit Card will never have the opportunity to play its now zero unpublished link. The result is no glitch.

So, if we designed our code in such a way that the link holder (CreditCard) is guaranteed to be released when the link object (Client) is released, then we have a scenario in which unpublished links were developed.

Thanks @Bob

-one
source

A quick search on this topic> Apple docs .

I assume that the argument for using unowned in this case is solely that it is assumed that unowned will never be nil (without a client).

-2
source

Source: https://habr.com/ru/post/1013033/


All Articles