← Back to Swift Course | Chapter 14: Standard Library & Best Practices | Lesson 6 of 7

Combine के Basics

Combine समय के साथ आने वाली values handle करने के लिए Apple का toolkit है, आप जो pipes एक साथ connect करते हैं उनसे बहते updates की एक stream जैसा।
Syntax
swift
import Combine

let cancellable = Just(value)
    .map { $0 transformation }
    .sink { received in
        // use received
    }

A Minimal Publisher and Subscriber

एक Just publisher एक single value wrap करता है और इसे .sink से attached किसी भी subscriber को तुरंत deliver करता है, इसे deterministically demonstrate करने के लिए सबसे simple possible Combine pipeline बनाते हुए।

Note:
  • Conceptual note: यह असली, syntactically correct Combine code है जैसे यह एक Apple platform (macOS/iOS) पर Xcode के साथ चलती।
  • Combine एक Apple-platform framework है और इसका module Linux पर open-source Swift toolchains में available नहीं है, इसलिए यह specific example इस course के Linux-based sandbox में execute नहीं हो सकता -- इसे यहां एक runnable के बजाय एक conceptual reference के रूप में treat करें।
Warning: Combine को एक Apple platform चाहिए (macOS/iOS with Xcode) और यह इस course के Linux-based code sandbox में नहीं चलता।

उदाहरण: A Minimal Publisher and Subscriber

markup
import Combine
var subscriptions = Set<AnyCancellable>()
let publisher = Just("Hello from Combine")
publisher.sink { value in
    print(value)
}.store(in: &subscriptions)

Transforming Values with map

Combine publishers emitted values को subscriber तक पहुंचने से पहले transform करने के लिए .map जैसे operators support करते हैं, Array.map के similar।

Warning: Combine को एक Apple platform चाहिए (macOS/iOS with Xcode) और यह इस course के Linux-based code sandbox में नहीं चलता।

उदाहरण: Transforming Values with map

markup
import Combine
var subscriptions = Set<AnyCancellable>()
let publisher = Just(5)
publisher.map { $0 * $0 }.sink { value in
    print("Squared: \(value)")
}.store(in: &subscriptions)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि एक Just publisher से Combine values कई run-loop turns में asynchronously आती हैं; एक Just publisher असल में अपनी single value subscriber को synchronously emit करता है।
  2. एक subscription के लिए एक strong reference (जैसे एक Set<AnyCancellable> में) रखना भूल जाना; इसे तुरंत deallocate होने देना pipeline को values deliver करने से रोक सकता है।
  3. एक one-off synchronous computation के लिए Combine उपयोग करना जहां एक plain function simpler होता; Combine समय के साथ values की ongoing streams के लिए चमकता है।
चैप्टर सारांश
  • Combine Publishers और Subscribers का उपयोग करके asynchronous event streams को model करता है।
  • .sink { } किसी publisher की values को सीधे एक closure के साथ subscribe करने का एक simple तरीका है।
  • एक Just publisher बिल्कुल एक value emit करता है और फिर तुरंत finish होता है।
  • असली Combine pipelines में आमतौर पर network calls या timers शामिल होते हैं, जिन्हें observe करने के लिए एक running event loop चाहिए -- यह example जानबूझकर सबसे simple possible synchronous publisher उपयोग करता है ताकि इसका output deterministic हो।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.