【发布时间】:2020-10-11 05:24:39
【问题描述】:
我有一个提取消息日志的 UIAnimation,但我还需要使用 for 循环填充消息日志。我在 for 循环开始之前发生了动画,但是,直到 for 循环完成执行后,动画才真正开始(即使循环位于 UIAnimation 之后)。
我尝试将 for 循环的内容放在自动释放池中(没有用)
我曾尝试在后台线程上运行 for 循环,但是,循环中的大部分代码都需要在主线程上执行,而这种方法我没有成功。
for循环很大,大部分代码与问题无关,所以你可以用任何UIAnimation来设置,任何相对耗时的for循环...
例如:
let someView = UIView(frame:CGRect(x:self.view.frame.size.width,y:0,width:self.view.frame.size.width,height:self.view.frame.size.height))
someView.backgroundColor = .red
UIView.animate(withDuration:0.3,animations:{
self.someView.frame.origin.x = 0
})
//You can wrap this with something like:
//DispatchQueue.global(qos: .background).async(execute: {
//But this^ doesn't work on the majority of the code within the loop
for i in 0...1000{
print("hopefully this loop takes a few seconds to finish executing")
print("mainthread tasks being executed")
print("populating UITable in my particular case, and rendering Images/Video/text")
}
我可以让它变得更好的最接近的方法是在我的 UIAnimation 中添加一个“完成”(这将允许 UIView 完全/立即退出)但是在第一个之前还有一秒钟的等待时间消息出现...
另外,我在 for 循环中填充了一个 UITableView。不确定这是否与我遇到的主要问题有任何额外的相关性......
我非常熟悉 DispatchQueue 以及异步/同步之间的区别。如果您有任何潜在的解决方案或想法,请告诉我。
【问题讨论】:
-
看看这个stackoverflow.com/questions/23912993/…可能会给你一些想法
标签: swift asynchronous async-await dispatch