Skip to content

Instantly share code, notes, and snippets.

@dry1lud
dry1lud / combine-retry.md
Last active November 27, 2023 10:07
Retry operation in Swift/Combine.

There is a function .retry() in Combine that helps to retry a request. Although it is not enough just to call retry() to achieve retring logic. The retry() function does not do another request but it re-subscribes only to a publisher. To make another request the tryCatch() might be used. In the code below if the first call fails there are three attempts to retry (retry(3)):

import UIKit
import Combine
import PlaygroundSupport

enum CustomNetworkingError: Error {
    case invalidServerResponse
}
@dry1lud
dry1lud / guard-statement.md
Last active November 4, 2019 10:41
Benefits of using a 'guard' statement in Swift language

It forces you to break ealier

  1. It has a positive test rather than negative one
  2. The else clause forces you to bring a program control outside the 'guard' scope (exit the function)
guard let var1 = par1 else { // else is mandatory
   return // must have. brings it outside the scope.
}
```swift
## Unlike the 'if' statement the variables declared at the condition block are visible in the whole 'guard' scope