Skip to content

Instantly share code, notes, and snippets.

@priore
Created May 19, 2020 13:20
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 priore/bcb341a8c1218b58016e1503803a3092 to your computer and use it in GitHub Desktop.
Save priore/bcb341a8c1218b58016e1503803a3092 to your computer and use it in GitHub Desktop.
Thread Safe Weak Ref Queue
import Foundation
class WeakRef<T> where T: AnyObject {
private let queue = DispatchQueue(label: "ThreadSafeWeakRef.queue", attributes: .concurrent)
private(set) weak var _object: T?
weak var object: T? {
var result: T?
queue.sync {
result = _object
}
return result
}
init(object: T?) {
queue.async(flags: .barrier) {
self._object = object
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment