[ad_1]
I’ve a PLP web page, I have to hit inventory API for all of the merchandise seen in display screen, and Have to replace the inventory UI for every product as soon as acquired the response for each product, I’ve applied this utilizing Dispatch Semaphore however confronted crash when navigating between screens and I attempted utilizing Operation queue however in that I get the response at cumulative time of all of the API hits. Even I attempted with Dispatch group, it additionally responds similar as Operation queue.
One API is taking round half second so every product inventory needs to be up to date in half to 1 second is what I want, Any assist appreciated.
Tried code utilizing Operation Queue
var queue: OperationQueue?
queue = OperationQueue()
queue?.qualityOfService = .background
productListView.sendCBRClosure = { [unowned self] cbrCodeArray in
queue?.maxConcurrentOperationCount = 5
for cbrCode in cbrCodeArray {
queue?.addOperation {
self.getStockDetail(cbrCode: cbrCode) // API name
}
}
}
Tried code utilizing Dispatch Semaphore
var semaphore = DispatchSemaphore(worth: 4)
productListView.sendCBRClosure = { [weak self] cbrCodeArray in
DispatchQueue.international().async { [weak self] in
for cbrCode in cbrCodeArray {
self?.getStockDetail(cbrCode: cbrCode) { qtyStr in
self?.semaphore.sign()
}
self?.semaphore.wait()
}
[ad_2]
