ios - SwiftUI在Thread中更新progressBar
问题描述
我正在尝试管理与 SwiftUI 的聊天。
我发现了一个CircularProgressBar
可以与 SwiftUI 一起使用,在示例中,它与Timer
. 如果我使用 Zip 提取更改计时器,则 UI 不会更新。
struct DetailView: View {
var selectedChat: Chat?
@State var progressBarValue:CGFloat = 0
var body: some View {
Group {
if selectedChat != nil {
VStack {
if progressBarValue < 1 {
CircularProgressBar(value: $progressBarValue)
}
else {
//Text("WELL DONE")
Text("\(UserData().getChat(withID: selectedChat!.id)!.allText.first!.text)")
}
}
} else {
VStack {
CircularProgressBar(value: $progressBarValue)
Text("Detail view content goes here")
}
}
}.navigationBarTitle(Text("\(selectedChat?.name ?? "")"))
.onAppear {
if let chat = self.selectedChat {
if chat.allText.count == 0 {
let exData = ExtractData()
if let path = chat.getUnzipPath()?.relativePath {
DispatchQueue.main.async {//with or without the behavior is the same
exData.manageExtractedZip(unzipPath: path) { progress in
if progress >= 1 {
var newChat = chat
newChat.allText = exData.allTexts
let userD = UserData()
userD.overrideChat(with: newChat)
print(exData.allTexts)
}
self.progressBarValue = CGFloat(progress)
print("progressBarValue: \(self.progressBarValue)") //This is printing well
}
}
}
}
else {
self.progressBarValue = 1
}
}
/* This is working
Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { timer in
self.progressBarValue += 0.1
print(self.progressBarValue)
if (self.progressBarValue >= 1) {
timer.invalidate()
}
}*/
}
}
}
struct DetailView_Previews: PreviewProvider {
static var previews: some View {
DetailView(selectedChat: UserData().chatData.first!)
}
}
如何让它发挥作用?
解决方案
推荐阅读
- speech-recognition - 从帧中对 MFCC 进行分组
- angular - Angular Redux 使用实体检索数据到状态
- jmeter - Jmeter Webdriver 测试结果显示在查看结果树中,但数据未显示在汇总报告中
- php - 带有 INNER JOIN 查询的 PHP 和 Mysql - 只看到标题,没有数据
- kubernetes - 为什么 Kubernetes ConfigMap 的 yaml 中的注释计入非官方的 1 MB 大小限制?
- c++ - 如何将 data[i].int 转换为变量
- java - 在服务中调用 DAO 时的 AbstractMethodError 和 null 映射器
- java - 从 android studio 中的基于 Web 的应用程序打印?
- sql-server - 为什么我的 .mdf 文件比我所有表的 UsedSpaceKB 总和大很多?
- c++ - 编译 C++ 源文件时出现链接器错误,未定义对类函数的引用