Skip to content

Instantly share code, notes, and snippets.

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 ankittlp/aac9eb63f5cf6279a4ca3445a2c9d4e4 to your computer and use it in GitHub Desktop.
Save ankittlp/aac9eb63f5cf6279a4ca3445a2c9d4e4 to your computer and use it in GitHub Desktop.
URLSessionDataTaskPublisher_Operators
var cancellable: AnyCancellable?
let url = URL(string: "https://jsonplaceholder.typicode.com/posts")!
cancellable = URLSession.shared.dataTaskPublisher(for: url)
.map { $0.data } // 1
.decode(type: [Post].self, decoder: JSONDecoder()) // 2
.replaceError(with: []) // 3
.eraseToAnyPublisher() // 4
.sink(receiveValue: { posts in
print(posts.count)
}) // 5
cancellable?.cancel() // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment