Home > Software design >  Using value inside MergeMany operator in another pipeline
Using value inside MergeMany operator in another pipeline

Time:07-06

I created this function for decoding locally saved data.

private func getLocalCertificates(_ data: [CovidCertificateEntity]) -> [CovidCertificateDomainItem]? {
    var newCertificates: [CovidCertificateDomainItem]?
    
    Publishers
        .MergeMany(
            data.map { result -> AnyPublisher<Result<EUDCC, ErrorType>, Never> in
                self.certificateToDelete = result.qrCodeValue ?? ""
                return repository.getCertificateDetails(result.qrCodeValue ?? "")
            })
        .map { result -> CovidCertificateDomainItem? in
            switch result {
            case .success(let eudcc):
                do {
                    return try CovidCertificateDomainItem(eudcc: eudcc, qrCode: self.certificateToDelete)
                }
                catch {
                    return nil
                }
                
            case.failure(_):
                return nil
            }
        }
        .compactMap { $0 }
        .collect()
        .sink { result in
            newCertificates = result.reversed()
        }
        .store(in: &cancellables)
    
    return newCertificates
}

I wanted to achieve that value result from data.map inside MergeMany operator is proceeded to .map operator, so I can use it in constructor of CovidCertificateDomainItem

I tried to made this with help variable certificateToDelete, but it always have last value from data.map.

Is there any way to achieve this?

CodePudding user response:

Pass a tuple. Instead of returning

repository.getCertificateDetails(result.qrCodeValue ?? "")

return a tuple:

(result, repository.getCertificateDetails(result.qrCodeValue ?? ""))

You will have many other adjustments to make in order to achieve that, but when you do, you'll be able to receive that tuple in map and thus have all the data you need.

  • Related