Is weak self needed for table view cell button closure

后端 未结 3 979
忘掉有多难
忘掉有多难 2021-01-27 04:17

In trying to avoid retain cycles, would using [weak self] in in a UITableViewCell button action be necessary? Example:

in ViewController\'s cellForRow<

相关标签:
3条回答
  • 2021-01-27 04:54

    The key line to think about is:

    var buttonAction: ((UITableViewCell) -> Void)?
    

    You are offering to store a function long-term in an instance property.

    Now think about who refers to / owns whom. The view controller owns its view which is-or-owns the table view which owns the cell. Meanwhile the cell owns the function. If the function refers strongly to any of the objects I just mentioned, that is a retain cycle. It is a classic retain cycle, the absolute model of how retain cycles get made.


    [I would like to add a word about how I test for these things. There's a really cheap and easy way: wrap your view controller up in a navigation controller plus a blank root view controller, so that you can push your view controller onto it. Implement deinit in your view controller. Now run the app, push your view controller, play with it for a bit, and pop it with the Back button. If deinit isn't called, you've got a retain cycle.]

    0 讨论(0)
  • 2021-01-27 05:03

    Yes, it is necessary to use unowned or weak to capture self in this case.

    • Your view controller will most likely have a strong reference to the UITableView
    • The table view has strong reference to it's UITableViewCells and
    • Each cell has strong reference to your buttonAction closure.

    Using self directly will have as an effect a retain cycle.

    This is actually pretty easy to test. Try to present the following view controller and dismiss it:

    class TestTableViewCell: UITableViewCell {
        var closure: (() -> Void)?
        
        deinit {
            print("TestTableViewCell deinit!")
        }
    }
    
    class TestTableViewController: UITableViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
            
            tableView.register(TestTableViewCell.self, forCellReuseIdentifier: "TestTableViewCellIdentifier")
        }
        
        deinit {
            print("TestTableViewController deinit!")
        }
        
        override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 1
        }
        
        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCellIdentifier", for: indexPath) as! TestTableViewCell
            
            cell.closure = { [weak self] in
                guard let self = self else { return }
                self.testFunction()
            }
            
            return cell
        }
        
        func testFunction() {}
    }
    
    // Test present
    let controller = TestTableViewController()
    present(controller, animated: true) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            self.dismiss(animated: true)
        }
    }
    

    You will have the following output:

    TestTableViewController deinit!
    TestTableViewCell deinit!
    

    Now presenting the same view controller without weak and you will see that there is no output, meaning that the deinit functions are not get called and the objects stay in the memory.

    0 讨论(0)
  • 2021-01-27 05:17

    if you're not using the object of tableviewcell and you only want execute action on the cell make this.

    var buttonAction: (() -> Void)?
    
    @IBAction func buttonPressed(_ sender: Any) {
         buttonAction?()
    }
    

    and on the cellForRow use this,

    cell.buttonAction = { [weak self] in
         self?.someFunction()
    }
    

    for check retain cycles, I usually use this option for check if the memory is retain or not.

    0 讨论(0)
提交回复
热议问题