[英]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. 使用
Alamofire.request
而不是self.alamoFireManager
,你很高兴不去捕捉自己。 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 如果你真的需要使用
self.alamoFireManager
,正如@Tapani所提到的,因为alamoFireManager
是self的属性,你需要捕获self并在闭包中使用[weak self]
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. alamoFireManager
是self
一个属性,所以如果它将闭包保存到属性并且你捕获self
那么就有一个保留周期。 If you are not sure you should always use weak self
. 如果你不确定你应该总是使用
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. 此外,
unowned self
是危险的,因为它不是可选的。 If it's nil
and you try to use it your app crashes. 如果它是
nil
并且您尝试使用它,您的应用程序崩溃。 You should use weak self
instead. 你应该使用
weak self
。
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. 你的问题是好的,简单的答案是,不,你不需要在你的响应处理程序中使用
[unowned self]
或[weak self]
来使Alamofire正常工作。 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. 无论您是在本示例中创建了自己的
SessionManager
,还是使用了利用SessionManager
单例的Alamofire.request
API,您都可以100%选择调用者。
The memory management of the request itself is all managed internally in Alamofire leveraging GCD queues. 请求本身的内存管理全部在Alamofire内部管理,利用GCD队列。 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. 如果您发出请求,只要您在内存中发出请求的
SessionManager
,该请求就会完成并调用完成处理程序。 The singleton SessionManager
obviously will not be released from memory. 单例
SessionManager
显然不会从内存中释放。
If you created your own
SessionManager
, make sure you don't release it before your request completes.如果您创建了自己的
SessionManager
,请确保在请求完成之前不释放它。
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. 这里的问题不应该是关于你是否需要为Alamofire使用无主或弱自我,而是为了你自己。 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. 例如,假设此示例中的
self
是Network
类。 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. 如果在请求处于运行状态时取消分配您的
Network
类,那么也将释放您的自定义alamoFireManager
实例,该实例将使底层URLSession
无效,而该URLSession
将取消您的请求。 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. 通常,您不希望发生这种情况,并且您希望在响应处理程序中捕获self以确保它不会在过早关闭。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.