Last active
May 7, 2022 20:25
-
-
Save ankittlp/aac9eb63f5cf6279a4ca3445a2c9d4e4 to your computer and use it in GitHub Desktop.
URLSessionDataTaskPublisher_Operators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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