R Pelzer
R Pelzer

Reputation: 1268

Using a [weak self] for a HTTP Request

I have a question regarding the need of using [weak self] in closures and HTTP requests.

As example we have a HTTP request who triggers a closure on completion:

func saveBla() {
    blaManager.saveBla(bla) { error in
        self.pay5euro()
    }
}

My questions is: Do I need to use a weak reference here or not? First of all I don't want to lose the response from the api call, after moving to an other page. Beside of that I don't want to create a retain cycle with a memory leak?

func saveBla() {
    blaManager.saveBla(bla) { [weak self] error in
        guard let strongSelf = self else { return }
        strongSelf.pay5euro()
    }
}

Is it really needed to use a [weak self] in this situation?

Upvotes: 1

Views: 844

Answers (1)

BrikerMan
BrikerMan

Reputation: 334

It depends on the relationship of your manager and your controller.

Conslusion: It will cause retain cycle if A owned B and B owned A without a weak reference.

class NetManager {
    func fire(completion: (_ result: Bool) -> Void) {
        completion(true)
    }
}


class controler: UIViewController {
    override func viewDidLoad() {
        let manager = NetManager()
        // In this case the Manager and Controller NOT own each other,
        // And the block will release after request finished, it is ok to not use weak.
        manager.fire { (result) in

        }
    }


    let manager = NetManager()
    func anOtherExample() {
        // In this case the controller own the manager,
        // manager own the controller, but manager will relase the controller after 
        // request finished, this will cause a delay to controller's relase, but it is still ok.
        manager.fire { (result) in

        }
    }
}

if your manages behave like this, then manager will own the controller and it will cause retain cycle when the controller owns manager.

class NetManager {

    var completion: ((_ result: Bool) -> Void)?

    func fire(completion: @escaping (_ result: Bool) -> Void) {
        self.completion = completion
    }
}

more details: https://krakendev.io/blog/weak-and-unowned-references-in-swift

Upvotes: 2

Related Questions