Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 列表滚动后计时器中断_Ios_Swift_Xcode_Swiftui_Combine - Fatal编程技术网

Ios 列表滚动后计时器中断

Ios 列表滚动后计时器中断,ios,swift,xcode,swiftui,combine,Ios,Swift,Xcode,Swiftui,Combine,我有一个带计时器的类(每毫秒更新一次) 我还有一个TimerView对象的列表() List() { ForEach(self.timers) { timer in TimerPlusView(timer: timer) } } 在每个对象中,我都有一个文本,它会根据计时器更新其内容 Text("\(currentTime.timeIntervalSince(timer.time ?? Date()))") .font(.largeTitle)

我有一个带计时器的类(每毫秒更新一次)

我还有一个TimerView对象的列表()

List() {
    ForEach(self.timers) { timer in
        TimerPlusView(timer: timer)
    }
}
在每个对象中,我都有一个文本,它会根据计时器更新其内容

Text("\(currentTime.timeIntervalSince(timer.time ?? Date()))")
    .font(.largeTitle)
    .fontWeight(.bold)
    .foregroundColor(.black)
    .opacity(0.5)
    .onReceive(timeCount.currentTimePublisher) { newCurrentTime in
        self.currentTime = newCurrentTime
    }
事情是,在将列表滚动大约100px后(不是在此时),计时器停止工作,标签停止更新,我不知道为什么

我得到的行为:

UPD:这里有一个完整项目的链接供参考。


我认为问题在于Xcode中的一个bug。或者至少是一些非常不成文的东西。或者只是个谜

当从表视图重新使用视图(像旧的出列内容)时,问题似乎会出现,这会导致订阅丢失,并且不再发布计时器事件

尽管如此,我还是找到了一个类似工作的地方

TimerPlusView.swift
中,将
.onReceive
处理程序移动到body视图的最底部,例如

var body: some View {

    Button(action: {
        self.timer.title = "Tapped"
    }) {
        VStack(alignment: .leading) {
            Text(timer.title ?? "New Timer")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(.black)
            Text("\(currentTime.timeIntervalSince(timer.time ?? Date()))")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(.black)
                .opacity(0.5)
            // onReceive doesn't seem to work here:
            //.onReceive(timeCount.currentTimePublisher) { newCurrentTime in
            //  self.currentTime = newCurrentTime
            // }

        }
    }
    .buttonStyle(BorderlessButtonStyle())
    .contextMenu {
        Button(action: {
            self.context.delete(self.timer)
        }) {
            Text("Delete")
        }
    }
    // here, onReceive works:
    .onReceive(timeCount.currentTimePublisher) { newCurrentTime in
      self.currentTime = newCurrentTime
    }
}

我认为问题在于Xcode中的一个bug。或者至少是一些非常不成文的东西。或者只是个谜

当从表视图重新使用视图(像旧的出列内容)时,问题似乎会出现,这会导致订阅丢失,并且不再发布计时器事件

尽管如此,我还是找到了一个类似工作的地方

TimerPlusView.swift
中,将
.onReceive
处理程序移动到body视图的最底部,例如

var body: some View {

    Button(action: {
        self.timer.title = "Tapped"
    }) {
        VStack(alignment: .leading) {
            Text(timer.title ?? "New Timer")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(.black)
            Text("\(currentTime.timeIntervalSince(timer.time ?? Date()))")
                .font(.largeTitle)
                .fontWeight(.bold)
                .foregroundColor(.black)
                .opacity(0.5)
            // onReceive doesn't seem to work here:
            //.onReceive(timeCount.currentTimePublisher) { newCurrentTime in
            //  self.currentTime = newCurrentTime
            // }

        }
    }
    .buttonStyle(BorderlessButtonStyle())
    .contextMenu {
        Button(action: {
            self.context.delete(self.timer)
        }) {
            Text("Delete")
        }
    }
    // here, onReceive works:
    .onReceive(timeCount.currentTimePublisher) { newCurrentTime in
      self.currentTime = newCurrentTime
    }
}

从你的代码中,我不知道为什么你的计时器停止更新。也许你可以发布一个链接到一个最小的xcode项目,以帮助其他人在他们的机器上复制它。也许这也是一个模拟器错误-你也在设备上尝试过吗?是的,我也没有。NP,这是一个项目链接,是的,我尝试过使用模拟器和真实设备-到处都是相同的交易。当滚动视图滚动时,另一个运行循环接管。您还应该将计时器添加到跟踪运行循环中。从你的代码中,我不知道为什么你的计时器停止更新。也许你可以发布一个链接到一个最小的xcode项目,以帮助其他人在他们的机器上复制它。也许这也是一个模拟器错误-你也在设备上尝试过吗?是的,我也没有。NP,这是一个项目链接,是的,我尝试过使用模拟器和真实设备-到处都是相同的交易。当滚动视图滚动时,另一个运行循环接管。您还应该将计时器添加到跟踪运行循环中。