I'm new to swift
Now I work with Dispatch and there is a situation when I need to go back to the main thread and I use clouser . So clouser keeps the object with a strong link in order to properly handle such a situation make capturing .
The question is what I am working on now with an example in which capturing does not look like this
private func fetchData() { if let url = imageURL { Dispatch.__dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0).async { let contentData = NSData(contentsOf: url as URL) DispatchQueue.main.async { if url == self.imageURL { if let imageData = contentData { self.image = UIImage(data: imageData as Data) } } else { print("ignored data returned from \(url)") } } } } } An example explains a smart developer, so I think that he specifically missed this capturing , but I do not understand why ...
I think it should be written like this
private func fetchData() { if let url = imageURL { Dispatch.__dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0).async { let contentData = NSData(contentsOf: url as URL) DispatchQueue.main.async { [weak weakSelf = self] in if url == weakSelf!.imageURL { if let imageData = contentData { weakSelf!.image = UIImage(data: imageData as Data) } } else { print("ignored data returned from \(url)") } } } So now it is not clear in which situation capturing to be written, and in which there is not
