简体   繁体   English

倒计时的多个计时器无法正常工作 | SwiftUI

[英]Multiple Timers counting down are not working properly | SwiftUI

I have an auction app running.我有一个拍卖应用程序正在运行。 I need several timers counting down in the UI, these timers have different end Dates, and the end seconds can be updated when SignalR receives a new value.我需要在 UI 中倒计时几个计时器,这些计时器有不同的结束日期,当 SignalR 收到新值时,可以更新结束秒数。

I have implemented running timers in my current solution, but sometimes and suddenly, they start having delays between counting down a second.我已经在我当前的解决方案中实现了运行计时器,但有时突然之间,它们开始在倒计时之间出现延迟。

The timers are inside these components called LotCard within the ForEach计时器位于ForEach中称为LotCard的这些组件中

ForEach($lotService.getLotListDto) { $item in
    LotCard(lotCardViewModel: $item.lotCardViewModel,
            lotDto: item,
            fnStartLotConnection: { value in lotService.initSingleLotCard(uID: value)})

}

This is the necessary code within these components:这是这些组件中的必要代码:

//MARK: Timer
@State var timeRemaining = 9999
let timerLotCard = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

HStack(spacing: 3){
    Image(systemName: "stopwatch")
    if(lotCardViewModel.showTimer){
        Text("\(timeRemaining.toTimeString())")
            .font(.subheadline)
            .onReceive(timerLotCard){ _ in
                if self.timeRemaining > 0 {
                    self.timeRemaining -= 1
                    
                    if(self.timeRemaining <= 0){
                        self.timerLotCard.upstream.connect().cancel()
                    }
                }else{
                    self.timerLotCard.upstream.connect().cancel()
                }
            }
    }
}

I guess it's a problem with threads and using many timers simultaneously with the same Instance but I am not an experienced developer using SwiftUI / Swift .我想这是线程的问题,并且在同一个Instance上同时使用多个计时器,但我不是使用SwiftUI / Swift的经验丰富的开发人员。

This is how my interface looks like:这是我的界面的样子:

界面

Thanks for your help.谢谢你的帮助。

I came up with this approach thanks to the comments in my question.由于我的问题中的评论,我想出了这种方法。 I hope it works and suits your problems.我希望它有效并适合您的问题。

First, I created a Published Timer , meaning every component will run the same Timer.首先,我创建了一个Published Timer ,这意味着每个组件都将运行相同的 Timer。

import Foundation
import Combine

class UIService : ObservableObject {
    
    static let shared = UIService()

    //MARK: Timer to be used for any interested party
    @Published var generalTimer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

}

Finally, I am using that Timer wherever I want to use it.最后,我在任何我想使用它的地方使用那个定时器。 In this case, I use it in every LotCard component.在这种情况下,我在每个 LotCard 组件中使用它。

struct LotCard: View {

//MARK: Observable Class
@EnvironmentObject var UISettings: UIService

//MARK: Time for the counting down
@State var timeRemaining = 9999

var body: some View {
    HStack{
        
        Text(timeRemaining.toTimeString())
            .onReceive(UISettings.generalTimer){ _ in
                if self.timeRemaining > 0 {
                    self.timeRemaining -= 1
                }
            }
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM