Last active
July 8, 2020 13:13
-
-
Save inso-/b5cf41c6f0a8179eab6153e2cbd2f292 to your computer and use it in GitHub Desktop.
static hashmap container
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
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