I want to try concurrent processing of an array for the first time. The compiler gives this warning: “Mutation of captured var ‘scaledImage’ in concurrently-executing code; this is an error in Swift 6”.
I then revised the code by copying the source array a local let variable; thus all the data are now local but the warning persisted. Since all the data are now local and the array “scaledImage” is a ‘sink’ array I don’t understand why the warning persists nor what to do about it.
// Concurrent processing 1st attempt
func processArrayConcurrently() async -> [UInt8] {
var scaledImage = [UInt8]()
scaledImage.reserveCapacity(self.backingImage!.count)
// constants for the data
let dMin = Double(self.backingMinMax[0])
let dRange = Double(self.backingRange)
let backingImage = self.backingImage!
let concurrentQueue = DispatchQueue(label: "com.example.concurrentQueue", attributes: .concurrent)
DispatchQueue.concurrentPerform(iterations: backingImage.count) { index in
let processedValue = UInt8(backingImage[index] * 2) // Example processing
concurrentQueue.async(flags: .barrier) { // WARNING next line
scaledImage[index] = processedValue
}
}
// Wait for all tasks to complete
concurrentQueue.sync(flags: .barrier) {}
return scaledImage
}