import Foundation
import Combine
class NetworkService {
private let baseURL = "https://api.example.com"
private var cancellables = Set<AnyCancellable>()
func fetchPosts() -> AnyPublisher<[Post], Error> {
guard let url = URL(string: "\(baseURL)/posts") else {
return Fail(error: URLError(.badURL))
.eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.decode(type: PostsResponse.self, decoder: JSONDecoder())
.map { $0.posts }
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
func searchPosts(query: String) -> AnyPublisher<[Post], Error> {
guard let url = URL(string: "\(baseURL)/posts/search?q=\(query)") else {
return Fail(error: URLError(.badURL))
.eraseToAnyPublisher()
}
return URLSession.shared.dataTaskPublisher(for: url)
.retry(3)
.map(\.data)
.decode(type: [Post].self, decoder: JSONDecoder())
.catch { error -> AnyPublisher<[Post], Error> in
print("Error fetching posts: \(error)")
return Just([])
.setFailureType(to: Error.self)
.eraseToAnyPublisher()
}
.receive(on: DispatchQueue.main)
.eraseToAnyPublisher()
}
}
struct PostsResponse: Decodable {
let posts: [Post]
}
struct Post: Decodable, Identifiable {
let id: Int
let title: String
let body: String
let authorId: Int
}
import Foundation
import Combine
class PostsViewModel: ObservableObject {
@Published var posts: [Post] = []
@Published var isLoading = false
@Published var errorMessage: String?
@Published var searchQuery = ""
private let networkService = NetworkService()
private var cancellables = Set<AnyCancellable>()
init() {
setupSearchDebounce()
}
func loadPosts() {
isLoading = true
errorMessage = nil
networkService.fetchPosts()
.sink(
receiveCompletion: { [weak self] completion in
self?.isLoading = false
if case .failure(let error) = completion {
self?.errorMessage = error.localizedDescription
}
},
receiveValue: { [weak self] posts in
self?.posts = posts
}
)
.store(in: &cancellables)
}
private func setupSearchDebounce() {
$searchQuery
.debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
.removeDuplicates()
.filter { !$0.isEmpty }
.flatMap { query in
self.networkService.searchPosts(query: query)
.catch { _ in Just([]) }
}
.assign(to: &$posts)
}
}
Combine provides a declarative Swift API for processing values over time, perfect for handling async events like network requests, user input, and timers. Publishers emit sequences of values, and subscribers receive them. Operators transform, filter, and combine streams. I use URLSession.dataTaskPublisher for network calls, chaining operators like map, decode, and catch for transformation and error handling. The @Published property wrapper creates publishers automatically. Combine's sink and assign subscribers connect publishers to UI or state. Cancellables manage subscription lifecycles. This reactive approach eliminates callback hell and makes async code linear and composable.