Swift在不同的类中合并发布者/订阅者

qv7cva1a  于 2023-04-19  发布在  Swift
关注(0)|答案(1)|浏览(121)

我正在遵循这个例子:https://www.swiftbysundell.com/basics/combine/(稍微修改了一下),它打印出1,2,3,正如我所期望的那样。

class MyCounter {
    var publisher: AnyPublisher<Int, Never> {
        subject.eraseToAnyPublisher()
    }

    private(set) var value = 0 {
        didSet { subject.send(value) }
    }

    private let subject = PassthroughSubject<Int, Never>()

    func increment() {
        value += 1
    }
}

class PrintCounter {
    
    let myCounter = MyCounter()

    init() {
        
        let cancellable = myCounter.publisher
            .sink { value in
                print(value)
            }

        myCounter.increment()
        myCounter.increment()
        myCounter.increment()
    }
}

let printCounter = PrintCounter()

我现在正在尝试发布其他值,计时器,位置坐标等,还没有成功。

class MyTimer {
    var subscriptions = Set<AnyCancellable>()

    var publisher: AnyPublisher<Int, Never> {
        subject.eraseToAnyPublisher()
    }

    private var value = 0 {
        didSet {
            subject.send(value)
        }
    }

    private let subject = PassthroughSubject<Int, Never>()
    
    init() {
        
        let currentDate = Date()
        
        Timer.publish(every: 1.0, on: .main, in: .common)
            .autoconnect()
            .map({ (output) in
                return output.timeIntervalSince(currentDate)
            })
            .map({ (timeInterval) in
                return Int(timeInterval)
            })
            .sink { (seconds) in
                print("MyTimer: \(seconds)")
                self.value = seconds
            }
            .store(in: &subscriptions)
    }
}

class PrintMyTimer {
    
    let myTimer = MyTimer()
    
    init() {
            
        let cancellable = myTimer.publisher
            .sink { value in
                print("PrintMyTimer: \(value)")
            }
    }
}

let printMyTimer = PrintMyTimer()

即使'value'在MyTimer中递增,它也不会显示在printMyTimer接收器(print语句)中。
因此,我只是想知道,如果我在正确的轨道上使用合并为这一目的开始。

wqnecbli

wqnecbli1#

合并是否是解决这类问题的最佳方法有点主观。有些人会说新的异步序列是未来。但Combine工作得很好,特别是当有多个订阅者时。
您的示例不起作用的原因是,当PrintMyTimer的初始化程序完成时,订阅超出了范围。这会自动取消订阅。就像在MyTimer中一样,您需要保存订阅以保持其活动:

class PrintMyTimer {
    var subscriptions = Set<AnyCancellable>()
    let myTimer = MyTimer()
    
    init() {
        myTimer.publisher
            .sink { value in
                print("PrintMyTimer: \(value)")
            }
            .store(in: &subscriptions)
    }
}

相关问题