Questions tagged [strong-reference-cycle]

8 questions
48
votes
3 answers

Swift delegation - when to use weak pointer on delegate

Can someone explain when and when not to use a 'weak' assignment to a delegate pointer in Swift, and why? My understanding is that if you use a protocol that is not defined as a class you cannot, nor want to, assign your delegate pointer to…
7
votes
0 answers

How can I find all closures?

We've totally forgotten to capture self and its properties when referencing it within a closure. (Note: the compiler didn't warn us.) Now our application is full with strong reference cycles. To fix them, we have to add the capture list to each…
Display Name
  • 4,502
  • 2
  • 47
  • 63
2
votes
2 answers

Is a strong reference cycle created in this simple case of using URLSession?

I'm a little confused about how strong references are created and when reference cycles occur. Here's a simple example: class Model { var foo: Data? func makeRequest(url: URL) { let task = URLSession.shared.dataTask(with: url) {…
2
votes
0 answers

Swift: Weakly reference function as closure?

In Swift, I'm currently facing the issue where I need to often wrap functions in closures in order to prevent strong reference cycles when passing on a class's function as a closure parameter. For example, in the scenario below: class A { var…
ImJustACowLol
  • 826
  • 2
  • 9
  • 27
1
vote
1 answer

Will referencing an array of instances of a class in a class create a strong reference cycle?

I have the following class: class Circle: CustomStringConvertible, Hashable, Equatable { ... var bonus5Circles = [Circle]() ... } Now, in most cases the bonus5Circles array will be empty, but in some cases it will contain some subset of…
1
vote
2 answers

Swift, why don't class methods need closure lists

If functions are essentially closures. Why don't methods of a class need closure lists when referencing self or another instance property within the closure. Is there a [unowned self] behind the scenes? For example: class MyClass{ func…
webbyweb
  • 385
  • 2
  • 11
0
votes
1 answer

Swift strong reference cycles with arrays

If I have a class A which has a variable x which is an array of class B, and class B which always has a variable y parent of class A, how do I set up to avoid strong reference cycles. I get an error if I put class A { weak var x = [B] ...} (even…
domc
  • 147
  • 1
  • 12
-1
votes
1 answer

Objective-C retain cycle between 2 objects

Here is the code: TestA *ta = [[TestA alloc] init]; TestB *tb = [[TestB alloc] init]; ta.b = tb; tb.a = ta; I tried to set ta = nil or tb = nil. It didn't work but ta.b = nil worked. Why?