진행중인 모든 작업의 실행이 완료되면 NSOperationQueue 할당을 해제하려고합니다. 지금까지 아래 코드를 작성했으나 waitUntilAllOperationsAreFinished
은 비동기 호출이며 nil을 얻는 operationQueue에서 보유 할 수 없습니다.대기열의 모든 작업이 끝나자 마자 NSOperationQueue 할당 해제
class DownloadOperationQueue: NSOperationQueue {
private static var operationsKeyPath: String {
return "operations"
}
deinit {
self.removeObserver(self, forKeyPath: "operations")
}
var completionBlock: (() -> Void)? {
didSet {
self.addObserver(self, forKeyPath: DownloadOperationQueue.operationsKeyPath, options: .New, context: nil)
}
}
override init() {
super.init()
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if let operationPath = keyPath where operationPath == DownloadOperationQueue.operationsKeyPath {
if self.operations.count == 0 {
NSOperationQueue.mainQueue().addOperationWithBlock({
self.completionBlock?()
})
}
}
}
}
완료되면 : 제로에 도달 할 때까지
- (void)deallocOperationQueue
{
[operationQueue waitUntilAllOperationsAreFinished];
operationQueue = nil;
}
이름에'wait'가있는 메소드가 비동기라고 생각하는 이유는 무엇입니까? – Avi
당신은 그 문제를 잘못된 길로 생각하고 있습니다. 큐에 대한 강력한 참조가 없을 때 큐가 dealloc됩니다. 참조를 더 이상 필요로하지 않을 때는'nil'을 참조로 설정해야합니다. 더 이상 대기열이 필요하지 않을 때는 참조를 설정하지 마십시오. – Avi
나는 프로젝트 전체에 살아있는 컨트롤러 클래스를 사용하고있다. dealloc 등에서 nil로 설정할 수 없습니다. –