Home > AI > IOS > Combine >

CurrentValueSubject

Example 1:

// PassthroughSubject
let cur = PassthroughSubject<Int, Never>()
cur.send(10)
cur.sink(receiveCompletion: {
    print("Receive completion \($0)")
}, receiveValue: {value in
    print("Receive value \(value)")
})
cur.send(30)
cur.send(40)
cur.send(completion: .finished)
cur.send(50)
// 30, 40


// CurrentValueSubject
let cur = CurrentValueSubject<Int, Never>(20)
cur.send(10)
cur.sink(receiveCompletion: {
    print("Receive completion \($0)")
}, receiveValue: {value in
    print("Receive value \(value)")
})
cur.send(30)
cur.send(40)
cur.send(completion: .finished)
cur.send(50)


// print
// 10, 30, 40, 
 

Leave a Reply