Skip to content

Instantly share code, notes, and snippets.

@4np
Last active May 15, 2020 08:21
Show Gist options
  • Save 4np/fd6981df487f5ed42426a9f07c1db32f to your computer and use it in GitHub Desktop.
Save 4np/fd6981df487f5ed42426a9f07c1db32f to your computer and use it in GitHub Desktop.

Revisions

  1. 4np revised this gist May 15, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions RetainCyclePlayground.swift
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ class TestClass {
    print("Inside block 1: \(String(describing: self?.retainCount))")

    self?.block2 = {
    // `self` is still weekly captured from `block1`.
    print("Inside block 2: \(String(describing: self?.retainCount))")
    }

    @@ -39,12 +40,12 @@ class TestClass {
    print("Starting Bar: \(retainCount)")

    block1 = { [weak self] in
    // Unwrapping `self`...
    // Unwrapping `self`.
    guard let self = self else { return }

    print("Inside block 1: \(self.retainCount)")

    // ...requires you to again weekly capture `self` in nested closures.
    // As you now have a strong reference to `self`, you to again weekly capture `self` in nested closures.
    self.block2 = { [weak self] in
    print("Inside block 2: \(String(describing: self?.retainCount))")
    }
  2. 4np revised this gist May 15, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions RetainCyclePlayground.swift
    Original file line number Diff line number Diff line change
    @@ -39,10 +39,12 @@ class TestClass {
    print("Starting Bar: \(retainCount)")

    block1 = { [weak self] in
    // Unwrapping `self`...
    guard let self = self else { return }

    print("Inside block 1: \(self.retainCount)")

    // ...requires you to again weekly capture `self` in nested closures.
    self.block2 = { [weak self] in
    print("Inside block 2: \(String(describing: self?.retainCount))")
    }
  3. 4np revised this gist May 15, 2020. No changes.
  4. 4np created this gist May 15, 2020.
    71 changes: 71 additions & 0 deletions RetainCyclePlayground.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,71 @@
    import Cocoa

    class TestClass {
    var name = ""
    var block1: (() -> Void)?
    var block2: (() -> Void)?

    var retainCount: Int {
    CFGetRetainCount(self)
    }

    init() {
    print("Initializing: \(retainCount)")
    }

    func foo() {
    print("Starting Foo: \(retainCount)")

    block1 = { [weak self] in
    print("Inside block 1: \(String(describing: self?.retainCount))")

    self?.block2 = {
    print("Inside block 2: \(String(describing: self?.retainCount))")
    }

    // Execute block 2
    self?.block2?()

    print("Finishing block 1: \(String(describing: self?.retainCount))")
    }

    // Execute block 1
    block1?()

    print("Finishing Foo: \(retainCount)")
    }

    func bar() {
    print("Starting Bar: \(retainCount)")

    block1 = { [weak self] in
    guard let self = self else { return }

    print("Inside block 1: \(self.retainCount)")

    self.block2 = { [weak self] in
    print("Inside block 2: \(String(describing: self?.retainCount))")
    }

    // Execute block 2
    self.block2?()

    print("Finishing block 1: \(self.retainCount)")
    }

    // Execute block 1
    block1?()

    print("Finishing Bar: \(retainCount)")
    }


    deinit {
    print("Deinitializing: \(retainCount)")
    }
    }

    var testClass: TestClass? = TestClass()
    testClass?.foo()
    testClass?.bar()
    testClass = nil