Search code examples
swiftuitimercombine

Combine Timer - setting timer


I'm not very familiar with Combine's Timer.publish(every:, on: , in: ). Essentially I'm looking to run a timer to call a func in my ObservableObject class. Is there a better way of doing this, not sure if I'm doing so correctly or if there's a cleaner way of doing the same.

class Example : ObservableObject {
    private var cancellables = Set<AnyCancellable>()
    var cancellable: AnyCancellable? = nil
    
    @Published var somedate: Date = Date()
    
    init() {
        cancellable = Timer
            .publish(every: 1, on: .main, in: .common)
            .autoconnect()
            .sink(receiveValue: { _ in
                self.update()
            })
    }
    
    func update() {
        let publisher =  Date()
        Just(publisher)
            .sink(receiveCompletion: { completion in
            }, receiveValue: { date in
                self.somedate = date
                print(self.somedate)
            }).store(in: &cancellables)
    }
}

The output seems correct.

2021-08-10 22:25:24 +0000
2021-08-10 22:25:25 +0000
2021-08-10 22:25:26 +0000
2021-08-10 22:25:27 +0000
2021-08-10 22:25:28 +0000
2021-08-10 22:25:29 +0000
2021-08-10 22:25:30 +0000
2021-08-10 22:25:31 +0000
2021-08-10 22:25:32 +0000
2021-08-10 22:25:33 +0000
2021-08-10 22:25:34 +0000
2021-08-10 22:25:35 +0000
2021-08-10 22:25:36 +0000

Solution

  • You can significantly reduce complexity in your update function. There's no need to create a new publisher with Just to get the date. Plus, you can already get that in your initial sink:

    class Example : ObservableObject {
        private var cancellable: AnyCancellable? = nil
        
        @Published var somedate: Date = Date()
        
        init() {
            cancellable = Timer
                .publish(every: 1, on: .main, in: .common)
                .autoconnect()
                .sink(receiveValue: { [weak self] value in
                    self?.update(date: value)
                })
        }
        
        func update(date: Date) {
            self.somedate = date
            print(self.somedate)
        }
    }