Skip to content

Instantly share code, notes, and snippets.

@moreindirection
Created September 3, 2020 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moreindirection/998e931e14176a04c80d64c6f1ae7f8a to your computer and use it in GitHub Desktop.
Save moreindirection/998e931e14176a04c80d64c6f1ae7f8a to your computer and use it in GitHub Desktop.
Branching with Combine
URLSession.shared.dataTaskPublisher(for: URL(string: BASE_URL + filename)!)
.flatMap { data, response in
let httpURLResponse = response as! HTTPURLResponse
switch httpURLResponse.statusCode {
case 404:
return Just("Unable to find file").eraseToAnyPublisher()
case (300..<399):
// this is a function that returns a publisher - it can make network requests, whatever
return processRedirect(data)
case (200..<299):
// now process data normally
default:
return Failure(errorType).eraseToAnyPublisher()
}
func processRedirect(redirectData: Data) -> AnyPublisher<String, Error> {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment