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
func attempt<T>(maximumRetryCount: Int = 3, delayBeforeRetry: DispatchTimeInterval = .seconds(2), _ body: @escaping () -> Promise<T>) -> Promise<T> { | |
var attempts = 0 | |
func attempt() -> Promise<T> { | |
attempts += 1 | |
return body().recover { error -> Promise<T> in | |
guard attempts < maximumRetryCount else { throw error } | |
return after(delayBeforeRetry).then(on: nil, attempt) | |
} | |
} | |
return attempt() |
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
func executeOnMain() { | |
if !Thread.isMainThread { | |
DispatchQueue.main.async(execute: {() -> Void in | |
executeOnMain() | |
}) | |
return | |
} | |
// do something | |
} |
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
precedencegroup OperationChaining { | |
associativity: left | |
} | |
infix operator ==> : OperationChaining | |
@discardableResult | |
func ==><T: Operation>(lhs: T, rhs: T) -> T { | |
rhs.addDependency(lhs) | |
return rhs | |
} |
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
class Account: NSObject { | |
var balance: Double | |
var id: Int | |
override init(id: Int, balance: Double) { | |
self.id = id | |
self.balance = balance | |
} | |
func withdraw(amount: Double) { |
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
//Processor #1: | |
while f == 0 { | |
OSMemoryBarrier() | |
print x | |
} | |
//Processor #2: | |
x = 42 | |
OSMemoryBarrier() | |
f = 1 |
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
//Processor #1: | |
while f == 0 { | |
print x | |
} | |
//Processor #2: | |
x = 42 | |
f = 1 |
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
var x = 100 | |
func calculate() { | |
var y = 0 | |
for i in 1...1000 { | |
y += i | |
} | |
x = y | |
} |
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
var title : [String] = [] | |
func write(_ text: String) { | |
let words = text.split(separator: " ") | |
DispatchQueue.main.async { | |
for word in words { | |
title.append(String(word)) | |
print(word) | |
} | |
} |
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
var title : [String] = [] | |
var lock = NSLock() | |
func write(_ text: String) { | |
let words = text.split(separator: " ") | |
lock.lock() | |
for word in words { | |
title.append(String(word)) | |
print(word) | |
} |
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
func write(_ text: String) { | |
let words = text.split(separator: " ") | |
for word in words { | |
title.append(String(word)) | |
} | |
} | |
write("Concurrency with Swift:") // Thread 1 | |
write("What could possibly go wrong?") // Thread 2 |
NewerOlder