Sobes.tech
Junior — Middle

Who is responsible for retaining the Presenter object using a strong reference in MVP architecture?

sobes.tech AI

Answer from AI

In MVP (Model-View-Presenter) architecture, the responsibility for holding a reference to the Presenter object with a strong reference usually lies with the View (for example, UIViewController).

The View creates the Presenter and holds a strong reference to it so that the Presenter is not deallocated while the View is active. At the same time, the Presenter usually holds a weak reference to the View to avoid cyclic references and memory leaks.

Example:

class ViewController: UIViewController {
    var presenter: Presenter! // strong reference

    override func viewDidLoad() {
        super.viewDidLoad()
        presenter = Presenter(view: self)
    }
}

class Presenter {
    weak var view: ViewController? // weak reference

    init(view: ViewController) {
        self.view = view
    }
}
Who is responsible for retaining the Presenter object… - sobes.tech