Hello it's my first time of using with combine with UIKit, and I dont understand why receiveValue is trigger when I call my function, before sink finished.
The code look like this in my viewModel:
var product = CurrentValueSubject<Product? ,Never>(nil)
var isCallFailed = CurrentValueSubject<Bool ,Never>(false)
func getScannedt(scanned: String) {
URLSession.shared
.dataTaskPublisher(for: request)
.receive(on: DispatchQueue.main)
.tryMap{(data, response) -> Data in
guard let httpResponse = response as? HTTPURLResponse, 200...209 ~= httpResponse.statusCode else {
throw NetworkError.responseError
}
return dataPreformatted text
}
.decode(type: Product.self, decoder: JSONDecoder())
.sink{completion in
switch completion {
case .failure(let err):
print("Error is \(err.localizedDescription)")
self.isCallFailed.send(true)
case .finished:
self.isCallFailed.send(false)
break
}
}
receiveValue: {[weak self] productData in
self?.product.send(productData)
.store(in: &cancellables)
}
}```
and in the view i subscribe to product to call an action when I receive a product
self.scanViewModel.getScanned(scanned: scanned)
self.scanViewModel.product
.sink{[unowned self] product in
self.doSomething(produit: product)
}
.store(in: &cancellables) ```
The problem here, I don't know why the function doSomething is trigger two time, one time when I call the function getScanned
who trigger directly the receiveValue (value receive is nil) , and second time when my receiveValue get data for real this time.
And how better trigger error, because currently when I have an error I throw and the logic stop, and my self.scanViewModel.product is not update, so the last product is still displayed.
This is why I usr another CurrentValueSubject
but I think it's not the best idea.
Is this normal, or I do something wrong.
Thanks for your help.
PS: I'm doing swift since 5 month so if you have good ressources on swift and combine I'll take it !