Last active
January 13, 2020 17:04
-
-
Save aainaj/3a00c4e2959c5ce678d9582004965110 to your computer and use it in GitHub Desktop.
self in dispatch queue doesn't cause retain cycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class ListViewController: UIViewController { | |
let label = UILabel(frame: CGRect(x: 100, y: 200, width: 300, height: 50)) | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
label.text = "Animating" | |
view.addSubview(label) | |
let controller = DetailViewController() | |
controller.definesPresentationContext = true | |
let navControll = UINavigationController(rootViewController: controller) | |
self.navigationController?.present(navControll, animated: true, completion: nil) | |
} | |
deinit { | |
print("List controller Deinit") | |
} | |
} | |
class DetailViewController: UIViewController { | |
var listTitle: (() -> String)? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
performAsyncTaskIntoConcurrentQueue(with: { | |
print("\n############") | |
print("###### All images are downloaded") | |
}) | |
} | |
func performAsyncTaskIntoConcurrentQueue(with completion: @escaping () -> ()) { | |
let queue = DispatchQueue(label: "com.queue.Concurrent", attributes: .concurrent) | |
let group = DispatchGroup() | |
for i in 1...5 { | |
group.enter() | |
queue.async { | |
let imageURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/0/07/Huge_ball_at_Vilnius_center.jpg")! | |
let _ = try! Data(contentsOf: imageURL) | |
print(self.view.description) | |
print("###### Image \(i) Downloaded ######") | |
group.leave() | |
} | |
} | |
group.notify(queue: DispatchQueue.main) { | |
completion() | |
} | |
} | |
deinit { | |
print("Detail controller got deallocated") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment