From 22f9ef8c1e2b676c03fc23f3a616c7d46f161892 Mon Sep 17 00:00:00 2001 From: Matt Greenfield Date: Tue, 8 May 2018 17:11:12 +0700 Subject: [PATCH] NSCache is thread safe, but NSMapTable is not #11 --- LocoKit/Base/Timelines/TimelineStore.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/LocoKit/Base/Timelines/TimelineStore.swift b/LocoKit/Base/Timelines/TimelineStore.swift index e6edb5c6..293dea36 100644 --- a/LocoKit/Base/Timelines/TimelineStore.swift +++ b/LocoKit/Base/Timelines/TimelineStore.swift @@ -21,6 +21,9 @@ open class TimelineStore { private let sampleMap = NSMapTable.strongToWeakObjects() private let processingQueue = DispatchQueue(label: "TimelineProcessing") + public var itemsInStore: Int { return mutex.sync { itemMap.count } } + public var samplesInStore: Int { return mutex.sync { sampleMap.count } } + public func object(for objectId: UUID) -> TimelineObject? { return mutex.sync { if let item = itemMap.object(forKey: objectId as NSUUID) { return item } @@ -52,11 +55,11 @@ open class TimelineStore { } open func add(_ timelineItem: TimelineItem) { - itemMap.setObject(timelineItem, forKey: timelineItem.itemId as NSUUID) + mutex.sync { itemMap.setObject(timelineItem, forKey: timelineItem.itemId as NSUUID) } } open func add(_ sample: LocomotionSample) { - sampleMap.setObject(sample, forKey: sample.sampleId as NSUUID) + mutex.sync { sampleMap.setObject(sample, forKey: sample.sampleId as NSUUID) } } public func process(changes: @escaping () -> Void) {