Sunday, October 15, 2023
HomeiOS Developmentios - Retry after Delay for scheduled activity in Mix

ios – Retry after Delay for scheduled activity in Mix


I need to schedule API in sure interval of time, and if that API fails I need to retry that api after sure time and if it hits max retry it ought to cease calling API, however whether it is success it has to proceed in scheduled time.

Thought:

  • two streams, Timer and dataTaskPublisher.
  • Timer will drive the dataTaskPublisher.
  • if dataTaskPublisher fails:
  • reschdeule timer with some delay()
  • if maxretry rely reached cease timer(which is able to cease dataTaskPublisher)

I’m kinda misplaced to orchestrate two streams, Any options for pattern can be very useful.
Apologies for my newbie tryout.

var cancellables: AnyCancellable
let interval = 2
let maxRetry = 2
let delay = 5


var timer = Timer.publish(each: TimeInterval(interval), on: .major, in: .frequent).autoconnect()
var session =  URLSession.shared.dataTaskPublisher(for: URL(string: "https://google.com")!).map(.information).eraseToAnyPublisher()

cancellables = timer.flatMap{ _ in
    return session.catch { _ -> AnyPublisher<Knowledge, URLError> in 
        return Publishers.Delay(upstream: session,
                                interval: .seconds(delay), tolerance: 1,
                                scheduler: RunLoop.major).print("retrying").retry(maxRetry).eraseToAnyPublisher() 
    }.eraseToAnyPublisher()
}.sink(receiveCompletion: { comp in
        if case let .failure(err) = comp {
            print("=============================That is error (err)")
            // This does not set off.  


        }
    }, receiveValue: { information in
        print("success")
        
       
    })



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments