为什么递归调度。main.ashync需要越来越长的时间?
因此,我在Swiftui中具有这种递归功能。每次迭代平均需要50毫秒的iPhone。整个事情将需要一分钟左右。当用户启动它时,他会期望这一点,并且在应用程序到达出口条件并完成之前,该应用程序无济于事。但是,我当然希望它尽快完成,我希望用户能够在不耐烦的情况下尽早中止。同样,在每次迭代之后,它都会更新线图,以向用户显示其进度。为了速度,我在这样的主线程上运行它:
.onChange(of: kickItOff) { value in
recursiveFunction()
}
func recursiveFunction() {
Dispatch.main.async() {
step1()
step2()
step3()
if !exit {
recursiveFunction()
}
}
}
迭代中的每个步骤,迭代本身必须串行执行。
这很快运行,UI不会被阻止。只是一个问题。 每次开始时,完成需要更长的时间才能完成。第四次跑步的时间是第一次的两倍。
我用路标仪器对其进行了介绍,并且可以看到递归函数中的每个步骤都不会将持续时间从迭代变为迭代。迭代之间的时间差距增加。
有人知道为什么会发生这种情况并希望有解决方案吗?
So I have this recursive function in SwiftUI. Each iteration takes on average 50 ms on a late model iPhone. The whole thing will take a minute or so. When the user kicks it off he expects this and has nothing else to do in the app until the exit condition is reached and it is done. But of course I want it to finish as soon as possible and I want the user to be able to abort it early if he becomes impatient. Also after each iteration it updates a line chart to show its progress to the user. For speed, I’m running it on the main thread like this:
.onChange(of: kickItOff) { value in
recursiveFunction()
}
func recursiveFunction() {
Dispatch.main.async() {
step1()
step2()
step3()
if !exit {
recursiveFunction()
}
}
}
Each step in an iteration and the iterations themselves must execute serially.
This runs fast and the UI is not blocked. Just one problem.
Each time it is kicked off it takes longer and longer to finish. The 4th run takes twice as long as the 1st.
I profiled it with the signpost instrument and can see that each step in the recursive function does not change duration from iteration to iteration. It is the time gap between iterations that increases.
Does anyone know why this is happening and hopefully have a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dispatchqueue.main是一个串行队列。异步调用永远不会返回和递归功能有效阻止主队列。如果您随后在主队列上设置退出,则永远不会执行。
DispatchQueue.main is a serial queue. The async call never returns and recursiveFunction effectively blocks the main queue. If you are subsequently setting exit on the main queue, that will never be executed.