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

Commit ee215ef

Browse files
committed
Add lock to prevent mutating array at the same time
1 parent a70f7c4 commit ee215ef

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Fuse/Classes/Fuse.swift

+6
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ extension Fuse {
293293
let pattern = self.createPattern(from: text)
294294

295295
var items = [SearchResult]()
296+
let itemsLock = NSLock()
296297

297298
let group = DispatchGroup()
298299
let count = aList.count
@@ -303,7 +304,9 @@ extension Fuse {
303304
self.searchQueue.async {
304305
for (index, item) in chunk.enumerated() {
305306
if let result = self.search(pattern, in: item) {
307+
itemsLock.lock()
306308
items.append((index, result.score, result.ranges))
309+
itemsLock.unlock()
307310
}
308311
}
309312

@@ -447,6 +450,7 @@ extension Fuse {
447450
let count = aList.count
448451

449452
var collectionResult = [FusableSearchResult]()
453+
let resultLock = NSLock()
450454

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

493+
resultLock.lock()
489494
collectionResult.append((
490495
index: index,
491496
score: totalScore / Double(scores.count),
492497
results: propertyResults
493498
))
499+
resultLock.unlock()
494500
}
495501

496502
group.leave()

0 commit comments

Comments
 (0)