Swift'te Combine vs async/await: Aşamalı Geçiş Desenleri

Swift'te Combine'dan async/await'e geçiş için kapsamlı kılavuz: aşamalı stratejiler, köprüleme desenleri ve iOS kod tabanlarında paradigma birlikte yaşamı.

Combine'dan async/await'e Swift'te birlikte yaşam desenleriyle göç

Swift Concurrency'nin async/await ile gelmesi, iOS'ta asenkron programlama uygulamalarını dönüştürdü. Combine kullanan projeler için göç sorusu doğal olarak ortaya çıkmaktadır. Her şeyin yeniden yazılması mı gerekir? İki yaklaşım birlikte var olabilir mi? Hangi desenler yumuşak bir geçişe olanak tanır? Bu kılavuz, Combine'ı aniden bırakmadan async/await'in benimsenmesine olanak tanıyan aşamalı geçiş stratejilerini ele almaktadır.

Bu kılavuzun kapsamı

Bu kılavuz, mevcut kod tabanlarına uygun çift yönlü köprüleme örnekleri ve birlikte yaşam stratejileriyle Combine'dan async/await'e aşamalı geçiş için somut desenler sunmaktadır.

Temel Farkları Anlamak

Bir göçe başlamadan önce, Combine'ı async/await'ten ayıran şeyi anlamak elzemdir. Bu iki yaklaşım farklı ihtiyaçlara yanıt verir ve bazı kullanım senaryoları Combine ile daha iyi karşılanmaya devam eder.

Combine'ın Zihinsel Modeli

Combine, veri akışı modeline dayanır. Bir Publisher zamanla değerler yayar, operatörler bu değerleri dönüştürür ve bir Subscriber nihai sonucu alır. Bu model, UI olayları, bildirimler veya WebSocket'ler gibi sürekli akışlar için öne çıkar.

CombineExample.swiftswift
// Event stream with Combine - stream-based model
import Combine

class SearchViewModel {
    @Published var searchText = ""
    private var cancellables = Set<AnyCancellable>()

    // Combine excels for continuous streams with transformations
    func setupSearch() {
        $searchText
            // Wait 300ms pause in typing
            .debounce(for: .milliseconds(300), scheduler: RunLoop.main)
            // Ignore consecutive duplicates
            .removeDuplicates()
            // Filter searches that are too short
            .filter { $0.count >= 3 }
            // Transform text into network request
            .flatMap { query in
                self.searchAPI(query: query)
                    // Local error handling
                    .catch { _ in Just([]) }
            }
            // Final subscription
            .sink { results in
                self.updateUI(with: results)
            }
            .store(in: &cancellables)
    }

    private func searchAPI(query: String) -> AnyPublisher<[SearchResult], Error> {
        // Network implementation
    }
}

Bu kod, Combine'ın gücünü göstermektedir: sürekli bir olay akışını işlemek için bildirim niteliğinde operatörleri zincirleme.

async/await'in Zihinsel Modeli

Async/await sıralı bir model benimser: bir işlem başlar, kod sonucunu bekler, ardından devam eder. Bu model, izole ağ istekleri veya dosya okumaları gibi tek seferlik işlemler için daha sezgiseldir.

AsyncAwaitExample.swiftswift
// One-off operations with async/await - sequential model
import Foundation

actor SearchService {
    // async/await excels for sequential operations
    func performSearch(query: String) async throws -> [SearchResult] {
        // Pre-validation - clear sequential reading
        guard query.count >= 3 else {
            return []
        }

        // Network request with await
        let url = URL(string: "https://api.example.com/search?q=\(query)")!
        let (data, response) = try await URLSession.shared.data(from: url)

        // Response verification
        guard let httpResponse = response as? HTTPURLResponse,
              httpResponse.statusCode == 200 else {
            throw SearchError.invalidResponse
        }

        // Result decoding
        let results = try JSONDecoder().decode([SearchResult].self, from: data)
        return results
    }
}

Okuma doğrusal, hatalar try ile doğal olarak yayılır ve yürütme akışı hemen anlaşılır hale gelir.

Hangi yaklaşımı ne zaman seçmeli

Combine, sürekli akışlar (UI olayları, zamanlayıcılar, WebSocket'ler) için geçerli kalmaktadır. Async/await, tek seferlik işlemler (API istekleri, dosya okuma, izole hesaplamalar) için daha uygundur.

Combine'dan async/await'e Köprü Kurmak

Bir göçün ilk adımı genellikle mevcut Publisher'ları async/await kodunda tüketmekten oluşur. Swift bu köprüleme için yerel araçlar sunar.

Publisher.values ile AsyncSequence Kullanımı

Swift 5.5'ten beri her Publisher, bir AsyncPublisher döndüren bir .values özelliği sunar. Bu asenkron dizi, yayılan değerler üzerinde bir for await döngüsüyle yineleme yapmaya olanak tanır.

BridgingCombineToAsync.swiftswift
// Publisher → AsyncSequence conversion via .values
import Combine

class NotificationObserver {
    private let notificationPublisher: AnyPublisher<Notification, Never>

    init() {
        // Existing Combine Publisher
        notificationPublisher = NotificationCenter.default
            .publisher(for: UIApplication.didBecomeActiveNotification)
            .eraseToAnyPublisher()
    }

    // Consuming the Publisher with async/await
    func observeNotifications() async {
        // .values converts the Publisher to AsyncSequence
        for await notification in notificationPublisher.values {
            // Process each notification
            await handleAppBecameActive(notification)
        }
        // This line is never reached for an infinite Publisher
    }

    private func handleAppBecameActive(_ notification: Notification) async {
        // Async processing logic
    }
}

Bu yaklaşım, orijinal Publisher'ı korurken aynı zamanda asenkron bağlamda tüketilmesine de olanak tanır.

firstValue ile Tek Bir Değer Almak

Tek bir değer yayan Publisher'lar (bir ağ isteği gibi) için .values.first(where:) özelliği veya özel bir uzantı köprülemeyi basitleştirir.

SingleValueBridging.swiftswift
// Extension to extract a single value from a Publisher
import Combine

extension Publisher where Failure == Never {
    // Awaits and returns the first emitted value
    var firstValue: Output {
        get async {
            await withCheckedContinuation { continuation in
                var cancellable: AnyCancellable?
                cancellable = self.first()
                    .sink { value in
                        continuation.resume(returning: value)
                        cancellable?.cancel()
                    }
            }
        }
    }
}

extension Publisher {
    // Throwing version for Publishers with errors
    var firstValueThrowing: Output {
        get async throws {
            try await withCheckedThrowingContinuation { continuation in
                var cancellable: AnyCancellable?
                cancellable = self.first()
                    .sink(
                        receiveCompletion: { completion in
                            if case .failure(let error) = completion {
                                continuation.resume(throwing: error)
                            }
                            cancellable?.cancel()
                        },
                        receiveValue: { value in
                            continuation.resume(returning: value)
                        }
                    )
            }
        }
    }
}

// Usage in async code
class UserRepository {
    private let apiClient: APIClient

    func fetchCurrentUser() async throws -> User {
        // Consume an existing Publisher asynchronously
        try await apiClient.userPublisher().firstValueThrowing
    }
}

Bu uzantı, köprüleme karmaşıklığını kapsüller ve temiz bir API sunar.

iOS mülakatlarında başarılı olmaya hazır mısın?

İnteraktif simülatörler, flashcards ve teknik testlerle pratik yap.

async/await'ten Combine'a Köprü Kurmak

Ters göç de aynı şekilde gereklidir: mevcut Combine pipeline'larında async kodunu tüketmek.

Bir async Fonksiyonundan Publisher Oluşturmak

En doğrudan yaklaşım, async çağrıyı kapsüllemek için bir Task ile birleştirilen Future kullanır.

BridgingAsyncToCombine.swiftswift
// async → Publisher conversion via Future
import Combine

extension Publisher {
    // async flatMap operator for Combine pipelines
    func asyncMap<T>(
        _ transform: @escaping (Output) async throws -> T
    ) -> AnyPublisher<T, Error> {
        flatMap { value in
            Future { promise in
                Task {
                    do {
                        // Execute the async transformation
                        let result = try await transform(value)
                        promise(.success(result))
                    } catch {
                        promise(.failure(error))
                    }
                }
            }
        }
        .eraseToAnyPublisher()
    }
}

// Usage in a Combine pipeline
class ImageProcessor {
    @Published var selectedImageURL: URL?
    private var cancellables = Set<AnyCancellable>()

    func setupProcessingPipeline() {
        $selectedImageURL
            .compactMap { $0 }
            // Use an async function in the Combine pipeline
            .asyncMap { url in
                // downloadImage is an async function
                try await self.downloadImage(from: url)
            }
            .asyncMap { imageData in
                // processImage is also async
                try await self.processImage(imageData)
            }
            .receive(on: DispatchQueue.main)
            .sink(
                receiveCompletion: { completion in
                    if case .failure(let error) = completion {
                        print("Error: \(error)")
                    }
                },
                receiveValue: { processedImage in
                    self.displayImage(processedImage)
                }
            )
            .store(in: &cancellables)
    }

    private func downloadImage(from url: URL) async throws -> Data {
        let (data, _) = try await URLSession.shared.data(from: url)
        return data
    }

    private func processImage(_ data: Data) async throws -> UIImage {
        // Async image processing
    }
}

Async Akışları için Özel Publisher

Daha gelişmiş ihtiyaçlar için özel bir Publisher tam bir AsyncSequence akışını kapsülleyebilir.

AsyncSequencePublisher.swiftswift
// Publisher wrapper for AsyncSequence
import Combine

struct AsyncSequencePublisher<S: AsyncSequence>: Publisher {
    typealias Output = S.Element
    typealias Failure = Error

    private let sequence: S

    init(_ sequence: S) {
        self.sequence = sequence
    }

    func receive<Sub>(subscriber: Sub) where Sub: Subscriber,
                                              Failure == Sub.Failure,
                                              Output == Sub.Input {
        let subscription = AsyncSubscription(
            sequence: sequence,
            subscriber: subscriber
        )
        subscriber.receive(subscription: subscription)
    }
}

private final class AsyncSubscription<S: AsyncSequence, Sub: Subscriber>: Subscription
where Sub.Input == S.Element, Sub.Failure == Error {

    private var task: Task<Void, Never>?
    private var subscriber: Sub?
    private let sequence: S

    init(sequence: S, subscriber: Sub) {
        self.sequence = sequence
        self.subscriber = subscriber
    }

    func request(_ demand: Subscribers.Demand) {
        // Start asynchronous iteration
        task = Task {
            do {
                for try await element in sequence {
                    // Check subscription is still active
                    guard subscriber != nil else { break }
                    _ = subscriber?.receive(element)
                }
                subscriber?.receive(completion: .finished)
            } catch {
                subscriber?.receive(completion: .failure(error))
            }
        }
    }

    func cancel() {
        task?.cancel()
        subscriber = nil
    }
}

// Convenience extension for any AsyncSequence
extension AsyncSequence {
    var publisher: AsyncSequencePublisher<Self> {
        AsyncSequencePublisher(self)
    }
}

Bir Kod Tabanında Birlikte Yaşam Stratejileri

Büyük bir kod tabanının tam göçü zaman alır. Combine ile async/await'i uyumlu bir şekilde birlikte yaşatacak desenler aşağıdadır.

Soyutlama ile Katmanlı Mimari

Uygulamayı soyutlayan protokoller tanımlamak, çağıran kodu değiştirmeden aşamalı göçe olanak tanır.

RepositoryAbstraction.swiftswift
// Abstraction enabling two implementations
import Combine

// Protocol defining the contract
protocol UserRepositoryProtocol {
    // Modern async interface
    func fetchUser(id: String) async throws -> User

    // Legacy Combine interface (optional with default implementation)
    func fetchUserPublisher(id: String) -> AnyPublisher<User, Error>
}

// Default Publisher implementation based on async
extension UserRepositoryProtocol {
    func fetchUserPublisher(id: String) -> AnyPublisher<User, Error> {
        Future { promise in
            Task {
                do {
                    let user = try await self.fetchUser(id: id)
                    promise(.success(user))
                } catch {
                    promise(.failure(error))
                }
            }
        }
        .eraseToAnyPublisher()
    }
}

// Modern implementation - async first
class UserRepository: UserRepositoryProtocol {
    private let apiClient: APIClient

    init(apiClient: APIClient) {
        self.apiClient = apiClient
    }

    func fetchUser(id: String) async throws -> User {
        // Native async implementation
        let url = URL(string: "https://api.example.com/users/\(id)")!
        let (data, _) = try await URLSession.shared.data(from: url)
        return try JSONDecoder().decode(User.self, from: data)
    }

    // fetchUserPublisher is provided by the default extension
}

Bu yaklaşım, eski kod Publisher'ları kullanmaya devam ederken yeni çağıranların async/await kullanmasına olanak tanır.

Bellek yönetimine dikkat

Köprüleme sırasında oluşturulan Task'lar, onları oluşturan nesnelerden daha uzun yaşayabilir. Bellek sızıntılarını önlemek için her zaman [weak self] kullanmak veya görevleri açıkça iptal etmek uygun olur.

Hibrit ViewModel

Bir ViewModel, geçiş döneminde her iki arayüzü de açıklayabilir.

HybridViewModel.swiftswift
// ViewModel supporting both Combine and async/await
import Combine
import SwiftUI

@MainActor
class ProfileViewModel: ObservableObject {
    // Published state for SwiftUI (Combine)
    @Published private(set) var user: User?
    @Published private(set) var isLoading = false
    @Published private(set) var errorMessage: String?

    private let repository: UserRepositoryProtocol
    private var cancellables = Set<AnyCancellable>()
    private var loadTask: Task<Void, Never>?

    init(repository: UserRepositoryProtocol) {
        self.repository = repository
    }

    // Async interface for modern UIKit or SwiftUI with .task
    func loadUser(id: String) async {
        isLoading = true
        errorMessage = nil

        do {
            user = try await repository.fetchUser(id: id)
        } catch {
            errorMessage = error.localizedDescription
        }

        isLoading = false
    }

    // Combine interface for legacy code
    func loadUserPublisher(id: String) {
        isLoading = true
        errorMessage = nil

        repository.fetchUserPublisher(id: id)
            .receive(on: DispatchQueue.main)
            .sink(
                receiveCompletion: { [weak self] completion in
                    self?.isLoading = false
                    if case .failure(let error) = completion {
                        self?.errorMessage = error.localizedDescription
                    }
                },
                receiveValue: { [weak self] user in
                    self?.user = user
                }
            )
            .store(in: &cancellables)
    }

    // Clean cancellation
    func cancelLoading() {
        loadTask?.cancel()
        cancellables.removeAll()
        isLoading = false
    }
}

Yaygın Combine Operatörlerinin Göçü

Bazı Combine operatörlerinin doğrudan async/await karşılığı yoktur. İşte bunları yeniden üretmenin yolları.

async ile Debounce Eşdeğeri

DebounceAsync.swiftswift
// Debounce implementation with async/await
import Foundation

actor Debouncer {
    private var task: Task<Void, Never>?
    private let duration: Duration

    init(duration: Duration) {
        self.duration = duration
    }

    // Cancels previous execution and schedules a new one
    func debounce(_ operation: @escaping @Sendable () async -> Void) {
        task?.cancel()

        task = Task {
            do {
                // Wait for the specified duration
                try await Task.sleep(for: duration)
                // Execute operation if not cancelled
                await operation()
            } catch {
                // Task cancelled - expected behavior
            }
        }
    }
}

// Usage in a ViewModel
@MainActor
class SearchViewModel: ObservableObject {
    @Published var searchText = ""
    @Published private(set) var results: [SearchResult] = []

    private let debouncer = Debouncer(duration: .milliseconds(300))
    private let searchService: SearchService

    init(searchService: SearchService) {
        self.searchService = searchService
    }

    func onSearchTextChanged(_ text: String) {
        Task {
            await debouncer.debounce { [weak self] in
                guard let self else { return }
                await self.performSearch(text)
            }
        }
    }

    private func performSearch(_ query: String) async {
        guard query.count >= 3 else {
            results = []
            return
        }

        do {
            results = try await searchService.search(query: query)
        } catch {
            // Error handling
        }
    }
}

TaskGroup ile Merge Eşdeğeri

MergeAsync.swiftswift
// Combining multiple async streams with TaskGroup
import Foundation

struct AsyncMerge {
    // Executes multiple async operations in parallel and returns all results
    static func merge<T>(
        _ operations: [@Sendable () async throws -> T]
    ) async throws -> [T] {
        try await withThrowingTaskGroup(of: T.self) { group in
            // Launch all operations in parallel
            for operation in operations {
                group.addTask {
                    try await operation()
                }
            }

            // Collect results
            var results: [T] = []
            for try await result in group {
                results.append(result)
            }
            return results
        }
    }

    // Streaming version that emits results as they arrive
    static func mergeStream<T: Sendable>(
        _ operations: [@Sendable () async throws -> T]
    ) -> AsyncThrowingStream<T, Error> {
        AsyncThrowingStream { continuation in
            Task {
                await withThrowingTaskGroup(of: T.self) { group in
                    for operation in operations {
                        group.addTask {
                            try await operation()
                        }
                    }

                    do {
                        for try await result in group {
                            continuation.yield(result)
                        }
                        continuation.finish()
                    } catch {
                        continuation.finish(throwing: error)
                    }
                }
            }
        }
    }
}

// Usage
class DataAggregator {
    func fetchAllData() async throws -> AggregatedData {
        // Execute three requests in parallel
        let results = try await AsyncMerge.merge([
            { try await self.fetchUsers() },
            { try await self.fetchPosts() },
            { try await self.fetchComments() }
        ])

        return AggregatedData(
            users: results[0] as! [User],
            posts: results[1] as! [Post],
            comments: results[2] as! [Comment]
        )
    }
}

iOS mülakatlarında başarılı olmaya hazır mısın?

İnteraktif simülatörler, flashcards ve teknik testlerle pratik yap.

Combine'ın Tercih Edildiği Kullanım Senaryoları

Async/await'in avantajlarına rağmen, bazı senaryolar Combine ile daha iyi karşılanmaya devam eder.

Reaktif UI Olay Akışları

SwiftUI ve UIKit, Combine operatörlerinin (debounce, throttle, combineLatest) parladığı sürekli olay akışları üretir.

UIEventsCombine.swiftswift
// Combine remains optimal for reactive UI events
import Combine
import SwiftUI

class FormViewModel: ObservableObject {
    @Published var email = ""
    @Published var password = ""
    @Published var confirmPassword = ""

    // Derived states computed via Combine
    @Published private(set) var isEmailValid = false
    @Published private(set) var isPasswordStrong = false
    @Published private(set) var passwordsMatch = false
    @Published private(set) var canSubmit = false

    private var cancellables = Set<AnyCancellable>()

    init() {
        setupValidation()
    }

    private func setupValidation() {
        // Email validation with debounce
        $email
            .debounce(for: .milliseconds(300), scheduler: RunLoop.main)
            .map { email in
                let regex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/
                return email.wholeMatch(of: regex) != nil
            }
            .assign(to: &$isEmailValid)

        // Password strength validation
        $password
            .map { password in
                password.count >= 8 &&
                password.rangeOfCharacter(from: .uppercaseLetters) != nil &&
                password.rangeOfCharacter(from: .decimalDigits) != nil
            }
            .assign(to: &$isPasswordStrong)

        // Password matching
        Publishers.CombineLatest($password, $confirmPassword)
            .map { password, confirm in
                !password.isEmpty && password == confirm
            }
            .assign(to: &$passwordsMatch)

        // Final combination to enable submit button
        Publishers.CombineLatest3($isEmailValid, $isPasswordStrong, $passwordsMatch)
            .map { $0 && $1 && $2 }
            .assign(to: &$canSubmit)
    }
}

Bu bildirimsel desen async/await ile çok daha ayrıntılı olurdu.

WebSocket Bağlantılarının Yönetimi

WebSocket'ler sürekli olarak mesajlar yayar, bu Combine için doğal bir kullanım senaryosudur.

WebSocketCombine.swiftswift
// WebSocket with Combine for continuous stream
import Combine
import Foundation

class WebSocketManager: ObservableObject {
    @Published private(set) var messages: [ChatMessage] = []
    @Published private(set) var connectionState: ConnectionState = .disconnected

    private var webSocketTask: URLSessionWebSocketTask?
    private let messageSubject = PassthroughSubject<ChatMessage, Never>()
    private var cancellables = Set<AnyCancellable>()

    // Exposed Publisher for consumers
    var messagePublisher: AnyPublisher<ChatMessage, Never> {
        messageSubject.eraseToAnyPublisher()
    }

    func connect(to url: URL) {
        webSocketTask = URLSession.shared.webSocketTask(with: url)
        webSocketTask?.resume()
        connectionState = .connected

        // Start reception loop
        receiveMessages()

        // Message processing pipeline
        messageSubject
            // Buffer messages to avoid too frequent UI updates
            .collect(.byTime(RunLoop.main, .milliseconds(100)))
            // Accumulate in history
            .scan([ChatMessage]()) { accumulated, new in
                accumulated + new
            }
            .assign(to: &$messages)
    }

    private func receiveMessages() {
        webSocketTask?.receive { [weak self] result in
            switch result {
            case .success(let message):
                if case .string(let text) = message,
                   let data = text.data(using: .utf8),
                   let chatMessage = try? JSONDecoder().decode(ChatMessage.self, from: data) {
                    self?.messageSubject.send(chatMessage)
                }
                // Continue reception
                self?.receiveMessages()

            case .failure(let error):
                self?.connectionState = .error(error.localizedDescription)
            }
        }
    }
}

Aşamalı Geçiş Kontrol Listesi

Başarılı bir göç metodik bir yaklaşım izler. Önerilen aşamalar şunlardır.

Aşama 1: Hazırlık

  • ✅ Kod tabanında kullanılan Publisher'ları belirleyin
  • ✅ Kategorize edin: sürekli akışlar vs tek seferlik işlemler
  • ✅ Köprüleme uzantıları oluşturun (firstValue, asyncMap)
  • ✅ Repository'ler için soyut protokoller tanımlayın

Aşama 2: Tek Seferlik İşlemlerin Göçü

  • ✅ Basit ağ isteklerini async/await'e dönüştürün
  • ✅ Dosya okumalarını taşıyın
  • ✅ Veritabanı işlemlerini dönüştürün
  • ✅ Varsayılan uygulamalar aracılığıyla Publisher'ları koruyun

Aşama 3: ViewModel'lerin Adaptasyonu

  • ✅ Mevcut ViewModel'lere async metotlar ekleyin
  • ✅ Yeni ekranlar için SwiftUI'de .task kullanın
  • ✅ Uyumluluk için @Published bağlantılarını koruyun

Aşama 4: Temizleme

  • ✅ Gereksiz hale gelen Combine metotlarını kaldırın
  • ✅ Kullanılmayan köprüleme uzantılarını silin
  • ✅ Bilerek korunan Combine desenlerini belgeleyin

Sonuç

Combine'dan async/await'e göç, modern Swift projeleri için doğal bir evrimi temsil eder. Çift yönlü köprüleme desenleri kullanan aşamalı yaklaşım, sert bir kopuşa neden olmadan async/await'in avantajlarının benimsenmesine olanak tanır.

Hatırlanması gereken anahtar noktalar:

  • ✅ Combine ve async/await farklı ihtiyaçlara yanıt verir
  • .values bir Publisher'ı AsyncSequence'e dönüştürür
  • Future + Task async kodu bir Publisher'da kapsüller
  • ✅ Soyut protokoller birlikte yaşamı kolaylaştırır
  • ✅ Combine reaktif UI akışları için geçerli kalır
  • ✅ Debounce gibi operatörler async'te yeniden oluşturulabilir
  • ✅ Aşamalı göç regresyon risklerini azaltır

Amaç Combine'ı ortadan kaldırmak değil, her bağlam için doğru aracı seçmektir: tek seferlik işlemler için async/await, sürekli olay akışları için Combine.

Pratik yapmaya başla!

Mülakat simülatörleri ve teknik testlerle bilgini test et.

Etiketler

#swift
#ios
#combine
#async-await
#migration

Paylaş

İlgili makaleler