I am trying to sort a very large array in my mobile app. I am using SwiftUI and would like the array to be sorted once the user taps on a button. This sorting can take about 3 seconds, so I would like this to happen off the main queue. However even when calling the code to happen on a global
async
block it still seems to happen on thread 1 and slow down my UI. During this time CPU spikes to 100% and the UI is unresponsive.
Button("Sort List") {
DispatchQueue.global(qos: .background).async {
self.updateList()
}
}
...
func updateList() {
//have tried putting the queue here as well
var sorted: [OBJ] = []
for obj in self.allObjs {
for i in 0..<self.selected.count {
...
let i = sorted.insertionIndexOf(obj, isOrderedBefore: self.sorter)
sorted.insert(obj, at: i)
}
}
self.someStateVar = sorted
}