Skip to content
This repository has been archived by the owner on May 8, 2022. It is now read-only.

Commit

Permalink
Add lock to prevent mutating array at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
chuganzy committed Jul 31, 2018
1 parent a70f7c4 commit ee215ef
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Fuse/Classes/Fuse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ extension Fuse {
let pattern = self.createPattern(from: text)

var items = [SearchResult]()
let itemsLock = NSLock()

let group = DispatchGroup()
let count = aList.count
Expand All @@ -303,7 +304,9 @@ extension Fuse {
self.searchQueue.async {
for (index, item) in chunk.enumerated() {
if let result = self.search(pattern, in: item) {
itemsLock.lock()
items.append((index, result.score, result.ranges))
itemsLock.unlock()
}
}

Expand Down Expand Up @@ -447,6 +450,7 @@ extension Fuse {
let count = aList.count

var collectionResult = [FusableSearchResult]()
let resultLock = NSLock()

stride(from: 0, to: count, by: chunkSize).forEach {
let chunk = Array(aList[$0..<min($0 + chunkSize, count)])
Expand Down Expand Up @@ -486,11 +490,13 @@ extension Fuse {
continue
}

resultLock.lock()
collectionResult.append((
index: index,
score: totalScore / Double(scores.count),
results: propertyResults
))
resultLock.unlock()
}

group.leave()
Expand Down

0 comments on commit ee215ef

Please sign in to comment.