Contents

retry(_:)

Attempts to recreate a failed subscription with the upstream publisher up to the number of times you specify.

Declaration

func retry(_ retries: Int) -> Publishers.Retry<Self>

Parameters

  • retries:

    The number of times to attempt to recreate the subscription.

Return Value

A publisher that attempts to recreate its subscription to a failed upstream publisher.

Discussion

Use retry(_:) to try a connecting to an upstream publisher after a failed connection attempt.

In the example below, a URLSession.DataTaskPublisher attempts to connect to a remote URL. If the connection attempt succeeds, it publishes the remote service’s HTML to the downstream publisher and completes normally. Otherwise, the retry operator attempts to reestablish the connection. If after three attempts the publisher still can’t connect to the remote URL, the catch(_:) operator replaces the error with a new publisher that publishes a “connection timed out” HTML page. After the downstream subscriber receives the timed out message, the stream completes normally.

struct WebSiteData: Codable {
    var rawHTML: String
}

let myURL = URL(string: "https://www.example.com")

cancellable = URLSession.shared.dataTaskPublisher(for: myURL!)
    .retry(3)
    .map({ (page) -> WebSiteData in
        return WebSiteData(rawHTML: String(decoding: page.data, as: UTF8.self))
    })
    .catch { error in
        return Just(WebSiteData(rawHTML: "<HTML>Unable to load page - timed out.</HTML>"))
}
.sink(receiveCompletion: { print ("completion: \($0)") },
      receiveValue: { print ("value: \($0)") }
 )

// Prints: The HTML content from the remote URL upon a successful connection,
//         or returns "<HTML>Unable to load page - timed out.</HTML>" if the number of retries exceeds the specified value.

After exceeding the specified number of retries, the publisher passes the failure to the downstream receiver.

See Also

Handling errors