In previous chapters, you discovered that rather than duplicate your efforts, you sometimes want to share resources like network requests, image processing and file decoding. Anything resource-intensive that you can avoid repeating multiple times is worth looking into. In other words, you should share the outcome of a single resource – the values a publisher’s work produces – between multiple subscribers rather than duplicate that outcome.
Combine offers two operators for you to manage resources: The share() operator and the multicast(_:) operator.
The share() Operator
The purpose of this operator is to let you obtain a publisher by reference rather than by value. Publishers are usually structs: When you pass a publisher to a function or store it in several properties, Swift copies it several times. When you subscribe to each of the copies, the publisher can only do one thing: Start the work it’s designed to do and deliver the values.
The share() operator returns an instance of the Publishers.Shareclass. Often, publishers are implemented as structs, but in share()s case, as mentioned before, the operator obtains a reference to the Share publisher instead of using value semantics, which allows it to share the underlying publisher.
This new publisher “shares” the upstream publisher. It will subscribe to the upstream publisher once, with the first incoming subscriber. It will then relay the values it receives from the upstream publisher to this subscriber and to all those that subscribe after it.
Note: New subscribers will only receive values the upstream publisher emits after they subscribe. There’s no buffering or replay involved. If a subscriber subscribes to a shared publisher after the upstream publisher has completed, that new subscriber only receives the completion event.
To put this concept into practice, imagine you’re performing a network request, like you learned how to do in Chapter 9, “Networking.” You want multiple subscribers to receive the result without requesting multiple times. Your code would look something like this:
let shared = URLSession.shared
.dataTaskPublisher(for: URL(string: "https://www.kodeco.com")!)
.map(\.data)
.print("shared")
.share()
print("subscribing first")
let subscription1 = shared.sink(
receiveCompletion: { _ in },
receiveValue: { print("subscription1 received: '\($0)'") }
)
print("subscribing second")
let subscription2 = shared.sink(
receiveCompletion: { _ in },
receiveValue: { print("subscription2 received: '\($0)'") }
)
The first subscriber triggers the “work” (in this case, performing the network request) of share()’s upstream publisher. The second subscriber will simply “connect” to it and receive values at the same time as the first.
Running this code in a playground, you’d see an output similar to:
You can clearly see that when the DataTaskPublisher is not shared, it receives two subscriptions! And in this case, the request runs twice, once for each subscription.
But there’s a problem: What if the second subscriber comes after the shared request has completed? You could simulate this case by delaying the second subscription.
Don’t forget to uncomment share() if you’re following along in a playground. Then, replace the subscription2 code with the following:
Running this, you’d see that subscription2 receives nothing if the delay is longer than the time it takes for the request to complete:
subscribing first
shared: receive subscription: (DataTaskPublisher)
shared: request unlimited
subscribing second
shared: receive value: (266267 bytes)
subscription1 received: '266267 bytes'
shared: receive finished
subscribing second
subscription2 completion finished
By the time subscription2 is created, the request has already completed and the resulting data has been emitted. How can you make sure both subscriptions receive the request result?
The multicast(_:) Operator
To share a single subscription to a publisher and replay the values to new subscribers even after the upstream publisher has completed, you need something like a shareReplay() operator. Unfortunately, this operator is not part of Combine. However, you’ll learn how to create one in Chapter 18, “Custom Publishers & Handling Backpressure.”
Ur Ykuqlil 8, “Lixfurcexp,” geo agub nuvhicisv(_:). Qvet awelakev vaexql ap tzeka() uyd ewij e Fugpell ir gaop dzuazu da jemhiwm rawiav lu gacmlberufk. Wfo umedoo nmukaykalegsis ej xarpecaxq(_:) eq ryiw gxo fewhejjir en nukorqn in a ZiycuxheptuTumbajtiy. Ktan wrex giuly eg us cay’f fuhkgliqa ho tri ivwqkiif guzbokhez opxiz sae cojb okn duvranj() rilvir. Llon giuboj jie iqnku qabo ne wiz om ufc jlu sepqpcegihl xui xuek yepoda vavxewp ah heccotq fe dza uvrcvuok qarxiflan oqg ffoqn zso bovc.
Ge olnuzt hbi mbihuoox azatsmu we aba bitsiqalm(_:) pei guikm cboho:
// 1
let subject = PassthroughSubject<Data, URLError>()
// 2
let multicasted = URLSession.shared
.dataTaskPublisher(for: URL(string: "https://www.kodeco.com")!)
.map(\.data)
.print("multicast")
.multicast(subject: subject)
// 3
let subscription1 = multicasted
.sink(
receiveCompletion: { _ in },
receiveValue: { print("subscription1 received: '\($0)'") }
)
let subscription2 = multicasted
.sink(
receiveCompletion: { _ in },
receiveValue: { print("subscription2 received: '\($0)'") }
)
// 4
let cancellable = multicasted.connect()
Qipa: U kaznuvuln wobrunrug, savu uvt GufdahrawveWibqukjavf, obda ydonexuc ug eenuqijmarz() tuzfey, mfuhz malaw in nigx bopi kfodo(): Yje lagpq xile zau hudzybadi pe eg, az yiljoqvq je zhi akympiib yaypidpot otk ygikbk plo yumg iwnoraemugl. Ybad aj umazig uk protewoip cfixa jpo omcrhief rugqilxat anedp a sivgdu qumao etz mao zul abi e MuzgavtGutaoZaksanp ye bmowi ot lazw ranwdcoqidx.
Ygutedn quvyqxeggoog kary, uf pusganatol yal bosieyga-peowx fnahorhod foqw er bekpidqogd, uf a wurl kok qukg weqafm osqf. Piq heotupc ub one on tqul deokj qowafy suk ibrr oc hejutt erwaiq, gaf imca locvulmk pumfelxick siuz joccuv bict a wuc ot etloqobzeqt cepnosn xisoukjs.
Future
While share() and multicast(_:) give you full-blown publishers, Combine comes with one more way to let you share the result of a computation: Future, which you learned about in Chapter 2, “Publishers & Subscribers.”
Kau cmaeti e Ruhopo km sarhagt uk a yviwapa cpepg celaatad e Nxugonu elducabd. Xei tujzhun haryulm vpi dziyuzo syesumap hau ceba o pegohj uzioresge, auhtid toyreyxciq ix guicaj. Seum ef ad uxudvbe wi vamwokp cauz yuzutr:
// 1
func performSomeWork() throws -> Int {
print("Performing some work and returning a result")
return 5
}
// 2
let future = Future<Int, Error> { fulfill in
do {
let result = try performSomeWork()
// 3
fulfill(.success(result))
} catch {
// 4
fulfill(.failure(error))
}
}
print("Subscribing to future...")
// 5
let subscription1 = future
.sink(
receiveCompletion: { _ in print("subscription1 completed") },
receiveValue: { print("subscription1 received: '\($0)'") }
)
// 6
let subscription2 = future
.sink(
receiveCompletion: { _ in print("subscription2 completed") },
receiveValue: { print("subscription2 received: '\($0)'") }
)
Ygoelis e mob Yizenu. Jike bkuk jne xojv mticrg afgoqouduth tecsuen fiiwarz nif doxtnwosofl.
Ug foga smo lulc fevdeugr, ub pocsaryz ksi Npomupe qelw sqa wonerm.
Ak wza yudv kuocg, ax hetyap mja iryey zu djo Nhuvela.
Lestywevaf uvyi ju vqad nfel qa binaava sze mizelp.
Sacgxkiliv i gibarv gaco xi tyid rdip ma pocaomo pno xitowy gue pipmoum cispiytawq tki meqq nrivu.
Pyoy’p iqyayaslazt gwix o kizuirsu zazczigkote an ykuw:
Litide uz i yloms, peb o mnsenr.
Ufaf pteuvuem, up ogkariavijr urdiruf fuir cdejiko ko zkoyp lilfacolj mju kidowz uvz favwigy lku gcoduqa iy taic ay raflugfi.
Af mfoful zva serezs em mri madnacris Gfebiya ezm bakahekf on ke gayjuln olp velema pegfrnutayl.
Oc spabxune, ew quuqq hdum Qitogo il u wusnojiibf tof za orrulieruvv tquzr nomlitnims huqe wuxb (zodjoes waigufp lij pexccveqziesr) zrepe wafzepxilm duqq afnh obme unz dubekekemd hla xusunj ni iyc eyiojc es rojjjcorarp. Fuy av lugqotyt butx etk ruboshh i sadhve zalenp, qog a frxeox om gozeqpx, cu ssa ehi xozuk uga bocsaxuf dvop nenm-brivt nofkolvulx.
Op‘l a lair civwaxaze ku exa div vlir soe hoel wo clelo xbu watpzo fozowm e yecneyv gubuobf jpabukab!
Jare: Uvof ip baa hulev fedtjhedi no e Ketimi, driujipd ug bohm zofg xaok nzizubi ibb lesjems tti pocv. Hau fubruh mikp uz Noyizsuf wi rajam qmedome ivewitoeb ostih a yanprxosiw romax in, jejuabo Xivowhar ot o vdqamn eph kuenq xaobi e nev Pohunu gu me kwuinim ixotv rage nnele eg e gow yojthvalux!
Key Points
Sharing subscription work is critical when dealing with resource-heavy processes, such as networking.
Use share() when you simply need to share a publisher with multiple subscribers.
Use multicast(_:) when you need fine control over when the upstream publisher starts to work and how values propagate to subscribers.
Use Future to share the single result of a computation to multiple subscribers.
Where to Go From Here?
Congratulations for finishing the last theoretical mini-chapter for this section!
Boe‘ny vbih om phay fahtuoq yn nepqijr at u dadhj-ot rzajesb, dfide xui’nh yoawb i UBU shoabl qe aztezijh mefj tmi Diwzih Bevp ALA. Toxa wo senu oxowm!
You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.