返回后,Swiftui 计时器未触发 [英] Swiftui timer not firing after navigating back

查看:37
本文介绍了返回后,Swiftui 计时器未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SwiftUI 中有一个计时器,它在第一次打开视图时起作用.返回并再次打开时,计时器不会启动.知道有什么问题吗?

i have a timer in SwiftUI which works when opening the view for the first time. When navigating back and opening again, the timer does not start. Any idea what can be wrong ?

import SwiftUI

struct ClockDetail: View {

    @State var seconds: Int = 0
    @ObservedObject var motion: MotionManager
    let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

    var body: some View {
        VStack(alignment: .leading) {
            Text("\(seconds)").font(.title).onReceive(timer) { output in
                self.seconds += 1
            }
            Text("Attitude Data").foregroundColor(.blue)
            Text("Roll: \(motion.roll)")
            Text("Pitch: \(motion.pitch)")
            Text("Yaw: \(motion.yaw)")
        }.onDisappear(perform: {
            self.timer.upstream.connect().cancel()
        })
    }
}


struct ClockDetail_Previews: PreviewProvider {
    static var previews: some View {
        ClockDetail(motion: MotionManager())
    }
}

我正在使用 .onDisappear 取消计时器,但似乎不起作用.

I'm using the .onDisappear to cancel the timer but seems that doesn't do the job.

推荐答案

你的 ClockDetail 只创建一次,所以一旦你使计时器无效,当你再次导航时它不再工作,因为视图是相同,但已经没有计时器.

Your ClockDetail is created only once, so once you invalidate timer it does not work anymore when you navigate again, because view is the same, but already w/o timer.

随着视图模型的引入,如下面的演示方法,我认为它更易于管理.

With the introduction of view model, as in below demo approach, it is better manageable I assume.

使用 Xcode 11.2/iOS 13.2 测试.请注意,我评论了对不可用实体的依赖关系,因为它们对于考虑的问题并不重要.

Tested with Xcode 11.2 / iOS 13.2. Note, I commented dependencies on not available entities, as they are not important for considered issue.

class ClockDetailViewModel: ObservableObject {
    @Published var seconds = 0
    private var subscriber: AnyCancellable?

    func setup() {
        self.seconds = 0
        self.subscriber = Timer
            .publish(every: 1, on: .main, in: .common)
            .autoconnect()
            .sink(receiveValue: { _ in
                self.seconds += 1
            })
    }

    func cleanup() {
        self.subscriber = nil
    }
}

struct ClockDetail: View {

    @State private var seconds: Int = 0
//    @ObservedObject var motion: MotionManager
    @ObservedObject private var vm = ClockDetailViewModel()

    var body: some View {
        VStack(alignment: .leading) {
            Text("\(vm.seconds)").font(.title)
            Text("Attitude Data").foregroundColor(.blue)
//            Text("Roll: \(motion.roll)")
//            Text("Pitch: \(motion.pitch)")
//            Text("Yaw: \(motion.yaw)")
            .onAppear {
                self.vm.setup()
            }
            .onDisappear {
                self.vm.cleanup()
            }
        }
    }
}

这篇关于返回后,Swiftui 计时器未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆