Which reference in a cycle should be weak/unowned?

I'm having trouble understanding which reference in cycle should be the weak/unowned one, even after reading the part in the Swift Language about it.
I'm writing an IRC chat client and you can imagine two entities in relationship with another, namely:

  • an IRCServer and a IRCChannel, each referencing another.

  • The server always outlives the channel.

To the best of my abilities, I think IRCChannel should have a unowned reference to IRCServer. Also judging by the book.
But does it make sense? Since the server always outlives the channel, should not the server have an unwoned reference to the channel? Because the channel is more ephemeral?
Maybe I'm thinking to much, and the idea is that if there is no server anymore, we don't want the channel to keep a reference to the server, and this prevent it from being deinitialzied.

There might be an argument for making both weak.

Imagine all of your clients' references expires, except for the one strong reference from the server. What good is a client that nobody but the server can use?

I think it would make sense if both had weak connections to each other. The server is kept alive by whoever owns the server, and the clients are kept alive by whoever owns the clients

1 Like

This feels more like a developer experience question. What experience do you want whoever is going to be using your library to have? What sort of relationships might be most intuitive for them? You should design your weak/strong/unowned references around that.

1 Like

But to generalize, the book example of Person and CreditCard, the idea being, a person may have a credit card, but a CreditCard always belongs to a person.
They made the Person reference of the CreditCard unowned, so that if a person gets out of scope, a credit card will do so as well immediately.
But I think I'm starting to understand also from @AlexanderM 's example. A way to think is "Given A and B, who owns each of them? Would I like A to survive if B is gnome? How about the opposite?"
Hope I'm getting it right.

1 Like

And then, imagine again, A and B, where A <---> B, if I want A not to prevent B from getting out of scope then it means it is A who should have the weak/unowned ref to B, and vice versa. And if both, then as Alexander sayed, both.
So think in terms of ownership, who owns what? And in terms of, would I like A to keep B alive, or should A's lifetime be irrelevant to B's lifetime?