如何从数组中取消分配所有引用元素? [英] How can I deallocate all references elements from an array?
问题描述
我正在尝试为我们的网络呼叫创建重试机制.我创建了2个班级.一个重试课程,另一个是经理,以防万一我想取消所有课程.
I'm trying to create a retrying mechanism for our network calls. I have created 2 classes. One a retry Class and another a Manager in case I wanted I can cancel all classes.
class Retry {
var url: String
var maxRetries: Int
init (url: String, retryCount: Int){
self.url = url
self.maxRetries = maxRetries
poll()
RetryManager.shared.add(self)
}
private func poll(){
guard retryCount == 0 else{
print("error")
}
getRequest()
}
private func getRequest(){
// make network request
// if no response is received poll again
}
func cancel(){
maxRetries = 0
}
}
经理班
class RetryManager{
static let sharedInstance = RetryManager()
var retries : [Retry?] = []
private init(){
}
func register(retry: Retry){
retries.append(retry)
}
func remove(retry: Retry){
retry.cancel() // XX; Do I also need this or just removing it is fine?
retries = retries.filter({$0 !== retry})
}
func cancelAll(){
retries.forEach({$0?.cancel()}) // ZZ; Do I also need this? or just removing them is fine?
retries.removeAll()
}
}
我的重试"实例将用于进行网络呼叫.
My Retry instances are to be used for making network calls.
我的主要问题是关于我的取消机制.用RetryManager.shared.cancelAll()
足以进行重新分配吗?还是我需要运行cancel或每个 cancel 实例(即XX,ZZ也必须)?
My major question is about my cancel mechanism. Will doing a RetryManager.shared.cancelAll()
suffice for deallocation? Or I need to run cancel or each cancel instance (ie XX, ZZ are also necessary)?
目前,一切正常,但是我不确定如果有多个指针,该如何工作……我需要做的事
Currently everything works fine, but I'm not sure if I how it would work if I have multiple pointers...would I need to do:
for index..<retries.count{
retries[index] = nil
}
据我所知,这无济于事,与执行retries.removeAll()
As far as I understand that won't help, it's same as doing retries.removeAll()
I also read In Swift, how do I set every element inside an array to nil? question but was told to open a new question
推荐答案
不确定我是否可以回答您的问题,但是我会尽我最大的理解:).
Not sure if I can answer your question, but I will try with my best understanding :).
Apple的自动引用计数(ARC)的手册)很好地涵盖了您的问题.
Apple's Swift handbook on Automatic Reference Counting (ARC) covers your question very well.
通常,您不需要具有可选的数组,
Usually you don't need to have an array of optionals,
var retries = [Retry]()
...
retries.removeAll()
将很好地删除所有包含的对象并删除对这些对象的引用.从上面介绍的上下文中,我不明白为什么需要声明一个可选数组.如您所知,Swift可选组件只是一个类型化的包装类Optional<Type>
,它不能解决内存分配问题.
will nicely remove all containing objects and delete the references to these objects. From your context presented above I do not understand why you need to declare an array of optionals. As you know Swift optionals under the hood are just a typed wrapper class Optional<Type>
, which doesn't work around the memory allocation problem.
该数组会将包含的对象的引用计数增加一个,即强引用.
The array will increase the contained objects' reference count by one, that is, a strong reference.
要确保要释放数组中的对象,必须使它们的引用计数等于零.如果没有其他东西引用所包含的对象,则将它们从数组中删除即可.
To ensure the objects in the array to be deallocated, one must make their reference count equal zero. Removing them from the array will do the trick, if nothing else is referencing the contained objects.
但是请注意参考周期.就您而言,您不应在Retry
实例中保留对retries
数组的引用.否则,即使将retries
数组设置为nil
,该数组及其包含的对象仍然相互之间具有强引用,这意味着它们的引用计数永远不会减少到零,从而导致内存泄漏.
Beware of the reference cycle though. In your case, you should not hold reference to the retries
array in Retry
instance. Otherwise even if you set the retries
array to nil
, the array and its contained objects still have strong reference to each other, meaning their reference counts will never reduce to zero, causing memory leak.
这篇关于如何从数组中取消分配所有引用元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!