Skip to content

Instantly share code, notes, and snippets.

@sabapathyk7
Created December 30, 2023 07:43
Show Gist options
  • Save sabapathyk7/f096d71673178ad4805e70a06c5d431f to your computer and use it in GitHub Desktop.
Save sabapathyk7/f096d71673178ad4805e70a06c5d431f to your computer and use it in GitHub Desktop.
AnyCancellable and Cancellable Example - Combine Framework
import Combine
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
// A simple publisher that emits integers from 1 to 5
let numberPublisher = [1, 2, 3, 4, 5].publisher
// Creating a Set of AnyCancellable to store cancellable instances
var cancellables = Set<AnyCancellable>()
// Subscribing to the publisher using sink
numberPublisher
.sink { completion in
print("Completed: \(completion)")
} receiveValue: { value in
print("Received value: \(value)")
}
.store(in: &cancellables) // Storing the AnyCancellable in the Set
// Another publisher that emits strings
let stringPublisher = ["A", "B", "C", "D", "E"].publisher
// Subscribing to the stringPublisher using sink
stringPublisher
.sink { completion in
print("Completed: \(completion)")
} receiveValue: { value in
print("Received value: \(value)")
}
.store(in: &cancellables) // Storing the AnyCancellable in the Set
// At some point, when you want to cancel all the subscriptions
cancellables.forEach { $0.cancel() }
let publisher = Just("Hello Combine")
let cancellable = publisher.sink(receiveValue: { value in
print(value)
})
cancellable.cancel()
final class TodoViewModel: ObservableObject {
var cancellables: Set<AnyCancellable> = []
func fetchData() {
let publisher = URLSession.shared.dataTaskPublisher(for: URL(string: "https://jsonplaceholder.typicode.com/todos/1")!)
.map(\.data)
.decode(type: Todo.self, decoder: JSONDecoder())
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
break // Do nothing on success
case .failure(let error):
print("Error: \(error)")
}
}, receiveValue: { todo in
// Handle the received data (e.g., update UI)
print("Received todo: \(todo.title)")
})
// Store the cancellable in the set
publisher.store(in: &cancellables)
}
}
struct Todo: Codable {
var userId: Int
var id: Int
var title: String
var completed: Bool
}
var viewModel = TodoViewModel()
viewModel.fetchData()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment