简体   繁体   中英

Do I need [unowned self] in AlamoFire, or is it already taken care of?

        let parameters = [
            "access_token": access_token,
        ]
        self.alamoFireManager!.request(.POST, CONSTANTS.APIEndpoint+"/auth", parameters: parameters).responseJSON { [unowned self]
            response in
            self.startWorking()
        }

Do I need unowned self inside the closure, or is it already taken care of by the library?

Use Alamofire.request instead of self.alamoFireManager and you are good to go without capturing self. If you really need to use self.alamoFireManager , as @Tapani mentioned, since alamoFireManager is a property of self, you need to capture self and use [weak self] in the closure

I was looking for the same answer you were looking for. I've found this answer. It mentions about an article about retain cycles. I think you don't need to capture self here.

alamoFireManager is a property of self so if it saves the closure to a property and you capture self then there is a retain cycle. If you are not sure you should always use weak self . That does no harm, but not using it when needed may cause a lot of trouble. Also, unowned self is dangerous because it is not optional. If it's nil and you try to use it your app crashes. You should use weak self instead.

You're question is a good one, and the simple answer is that no, you do not need to use [unowned self] or [weak self] in your response handlers to make Alamofire work properly. The choice is 100% up to you, the caller, whether you have created your own SessionManager as in this example, or whether you are using the Alamofire.request APIs which leverage the SessionManager singleton.

Alamofire Internals

The memory management of the request itself is all managed internally in Alamofire leveraging GCD queues. If you make a request, that request will complete and call the completion handler as long as the SessionManager you made the request on stays in memory. The singleton SessionManager obviously will not be released from memory.

If you created your own SessionManager , make sure you don't release it before your request completes.

Additional Thoughts

The question here shouldn't really be so much about whether you need to use unowned or weak self for Alamofire's sake, but for your own. Do you to guarantee that the request completes successfully? If yes, then you need to capture self here and avoid weak and unowned self to ensure the request completes. For example, let's say self in this example is a Network class. If you deallocate your Network class while a request is in flight, that will also deallocate your custom alamoFireManager instance which will invalidate the underlying URLSession which will in turn cancel your request. Generally, you don't want this to happen and you'd want to capture self inside the response handler to ensure it won't get shut down pre-maturely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM