Skip to content

Instantly share code, notes, and snippets.

@EricRabil
Created March 3, 2022 19:52
Show Gist options
  • Save EricRabil/ea1c6a49121455515ce5c27eb0669474 to your computer and use it in GitHub Desktop.
Save EricRabil/ea1c6a49121455515ce5c27eb0669474 to your computer and use it in GitHub Desktop.
Example of Swift Combine CurrentValueSubject
//
// main.swift
//
// Created by Eric Rabil on 3/3/22.
//
import Foundation
import Combine
let foo = CurrentValueSubject<String, Never>("bar")
var bag = Set<AnyCancellable>()
foo.sink { newValue in
print(newValue)
}.store(in: &bag)
foo.sink { newValue in
print(newValue)
}.store(in: &bag)
foo.send("baz")
/*
Output:
bar
bar
baz
baz
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment