This file contains hidden or 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
// Solution A: Unwind the recursion using DFS | |
func printXY(_ n: Int) -> [String] { | |
var explored = Set<Int>() | |
var stack = [n] | |
var output = [String]() | |
while let last = stack.last { | |
This file contains hidden or 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
class ViewControllerB: UIViewController { | |
var workItem: DispatchWorkItem? | |
override func viewDidLoad() { | |
let view = self.view | |
let workItem = DispatchWorkItem { | |
UIView.animate(withDuration: 1.0) { [weak self] in // this leaks | |
view?.backgroundColor = .red | |
} |
This file contains hidden or 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
let view = self.view | |
let workItem = DispatchWorkItem { | |
UIView.animate(withDuration: 1.0) { // adding [weak self] here will introduce a cycle | |
view?.backgroundColor = .red | |
} | |
} | |
self.workItem = workItem |
This file contains hidden or 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
let workItem = DispatchWorkItem { [weak self] in | |
UIView.animate(withDuration: 1.0) { | |
self?.view.backgroundColor = .red | |
} | |
} | |
self.workItem = workItem |
This file contains hidden or 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
let workItem = DispatchWorkItem { // <-- first closure | |
UIView.animate(withDuration: 1.0) { // <-- second closure | |
self.view.backgroundColor = .red | |
} | |
} | |
self.workItem = workItem |
This file contains hidden or 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
// Can you tell which version has a retain cycle? | |
// Version A | |
class ViewControllerA: UIViewController { | |
var workItem: DispatchWorkItem? | |
override func viewDidLoad() { | |
let workItem = DispatchWorkItem { | |
UIView.animate(withDuration: 1.0) { |
This file contains hidden or 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
let concurrent = DispatchQueue(label: "com.besher.concurrent", attributes: .concurrent) | |
concurrent.sync { | |
for _ in 0..<5 { print("🔵") } | |
} | |
concurrent.async { | |
for _ in 0..<5 { print("🔴") } | |
} |
This file contains hidden or 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
let concurrent = DispatchQueue(label: "com.besher.concurrent", attributes: .concurrent) | |
concurrent.async { | |
for _ in 0..<5 { print("🔵") } | |
} | |
concurrent.async { | |
for _ in 0..<5 { print("🔴") } | |
} |
This file contains hidden or 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
let serial1 = DispatchQueue(label: "com.besher.serial1") | |
let serial2 = DispatchQueue(label: "com.besher.serial2") | |
serial1.sync { // <---- we changed this to 'sync' | |
for _ in 0..<5 { print("🔵") } | |
} | |
// we don't get here until first loop terminates | |
serial2.async { | |
for _ in 0..<5 { print("🔴") } | |
} |
This file contains hidden or 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
let serial = DispatchQueue(label: "com.besher.serial") | |
serial.async { | |
for _ in 0..<5 { print("🔵") } | |
} | |
serial.async { | |
for _ in 0..<5 { print("🔴") } | |
} |
NewerOlder