Approach 1: -
let fetchedImageArray = [Images]()
let requestQueue = OperationQueue()
func fetchProductImage() {
let completionBlock = BlockOperation { [weak self] in
updateProductByFetchMediumImageResult()
}
fetchProductImageOperations.append(completionBlock)
var fetchProductImageOperations = [Operation]()
for index in 0...products.count - 1 {
let operation = FetchProductImageOperation(products[index], completion { image in
fetchedImageArray.append(image)
})
fetchProductImageOperations.append(operation)
completionBlock.addDependency(operation)
}
requestQueue.addOperations(fetchProductItemImageOperations, waitUntilFinished: false)
}
Approach 2: -
let isolationQueue = DispatchQueue(label: "com.humana.isolation", attributes: .concurrent)
var threadSafeProductsImage: [Images] {
get {
return isolationQueue.sync {
fetchedImageArray
}
}
set {
isolationQueue.async(flags: .barrier) {
self.fetchedImageArray = newValue
}
}
}
Approach 3:-
func fetchProductImage() {
let completionBlock = BlockOperation { [weak self] in
updateProductByFetchMediumImageResult()
}
fetchProductImageOperations.append(completionBlock)
let semaphore = DispatchSemaphore(value: 1)
var fetchProductImageOperations = [Operation]()
for index in 0...products.count - 1 {
let operation = FetchProductImageOperation(products[index], completion { image in
semaphore.wait()
self.threadSafeProductsImage.append(image)
semaphore.signal()
})
fetchProductImageOperations.append(operation)
completionBlock.addDependency(operation)
}
requestQueue.addOperations(fetchProductItemImageOperations, waitUntilFinished: false)
}
In my application I have list of product. And I have to fetch the image of each product and reassign to products. To achieve this I am using the above code.
I have tried three approach and and in each approach I have issue. Please help me to correct my code-
Approach 1 -
Have created an array to add web-service response. I do call the fetch web service as multiple operations and add responses into array. In this approach I do get crash when I append the response into array, as array is not thread safe. (Accessing thread safe array from multiple async call.)
Approach 2 -
In this approach I have converted array into a thread safe array with step 1 approach to add the image response into array. At this point of time I avoid the crash but there is issue as I do see lot of image response doesn’t gets append into image array after web service completion.
Approach 3 -
In this approach I have added semaphore wait and signal to sync the append response process. But in this approach I am facing deadlock condition and my background thread stuck and don’t process anything.
What am I missing or is there any better approach to perform the same approach.
The only requirement that I have to fetch the image of each product from list of products in multiple thread operations and then add those response into an array to use further.