Skip to content

Instantly share code, notes, and snippets.

@sarah-j-smith
Last active July 16, 2017 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sarah-j-smith/393b53b6ef6e8b208fb91cdbe5a1a61f to your computer and use it in GitHub Desktop.
Save sarah-j-smith/393b53b6ef6e8b208fb91cdbe5a1a61f to your computer and use it in GitHub Desktop.
class Request {
// DTO that can be accessed from different threads
// Note: this is faster/better/approved by Apple instead of @synchronized and
// other constructs (even tho' its a bit more verbose). DispatchQueue by
// default is Serial so this synchronizes accesses.
private var requestStateQueue = DispatchQueue(label: "requestStateQueue")
private var _cancelled = false
/** Set to true to cancel this request - atomic/threadsafe */
var cancelled: Bool {
get {
var result = false
requestStateQueue.sync {
result = _cancelled
}
return result
}
set {
requestStateQueue.sync {
_cancelled = newValue
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment