Skip to content

Commit

Permalink
NSCache is thread safe, but NSMapTable is not #11
Browse files Browse the repository at this point in the history
  • Loading branch information
sobri909 committed May 8, 2018
1 parent 6a30d4b commit 22f9ef8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions LocoKit/Base/Timelines/TimelineStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ open class TimelineStore {
private let sampleMap = NSMapTable<NSUUID, LocomotionSample>.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 }
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 22f9ef8

Please sign in to comment.