Skip to content

Instantly share code, notes, and snippets.

@inso-
Last active July 8, 2020 13:13
Show Gist options
  • Save inso-/b5cf41c6f0a8179eab6153e2cbd2f292 to your computer and use it in GitHub Desktop.
Save inso-/b5cf41c6f0a8179eab6153e2cbd2f292 to your computer and use it in GitHub Desktop.
static hashmap container
private struct PropertyData {
static private let accessQueue = DispatchQueue(label: "SynchronizedPropertyDataAccess", attributes: .concurrent)
static private var property = [Weak<AnyObject>: [Any]]()
static func memoryClean() {
Self.accessQueue.async(flags: .barrier) {
property = property.filter({$0.key.wrappedValue != nil})
}
}
static func get<T>(_ key: Weak<AnyObject>) -> T? {
Self.memoryClean()
var res: T?
self.accessQueue.sync {
res = Self.property[key]?.first { $0 is T } as? T
}
return res
}
static func set(_ key: Weak<AnyObject>, value: Any) {
Self.memoryClean()
Self.accessQueue.async(flags: .barrier) {
Self.property[key, default: []] += [value]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment