Swift 加载数据时UI未更新
在加载过程中,我试图从循环中更新我的UI(标签和进度条) 不过,在加载功能完成之前,UI不会更新 加载函数没有使用其他线程,我仍然在主线程中,因此据我所知,应该可以立即更新UI 我有什么遗漏吗 加载函数:(从ViewDidDisplay调用)Swift 加载数据时UI未更新,swift,Swift,在加载过程中,我试图从循环中更新我的UI(标签和进度条) 不过,在加载功能完成之前,UI不会更新 加载函数没有使用其他线程,我仍然在主线程中,因此据我所知,应该可以立即更新UI 我有什么遗漏吗 加载函数:(从ViewDidDisplay调用) func LoadDataFromDataCore(){ //1 警卫让appDelegate= UIApplication.shared.delegate作为?AppDelegate else{ 返回 } 让managedContext= appDele
func LoadDataFromDataCore(){
//1
警卫让appDelegate=
UIApplication.shared.delegate作为?AppDelegate else{
返回
}
让managedContext=
appDelegate.persistentContainer.viewContext
//2
让我们请求=
NSFetchRequest(entityName:“CDGlobeTile”)
//3
做{
让DataCoreResult=尝试managedContext.fetch(fetchRequest)
var LabelFromCDArray=[LabelTile]()
对于DataCoreResult.enumerated()中的(索引,CDGlobeTile){
updateLoadingBar(进度:浮点(索引)/浮点(DataCoreResult.count-1))
//从CoreData“解包”数据。。。
}
做{
请尝试managedContext.save()
}将let错误捕获为NSError{
打印(“保存上下文时出错。\(错误),\(错误.用户信息)”)
}
}将let错误捕获为NSError{
打印(“无法获取.\(错误),\(错误.userInfo)”)
}
打印(“已完成加载”)
}
updateLoadingBar-函数:
func updateLoadingBar(Progress: Float){
let percentage = Progress * 100
if let currentDispatch = OperationQueue.current?.underlyingQueue {
print(currentDispatch)
// this gives me "<OS_dispatch_queue_main: com.apple.main-thread>"
}
LoadingLabel.text = String(format: "%2.0f", percentage)
ProgressBar.progress = Progress
}
func updateLoadingBar(进度:浮动){
百分比=进度*100
如果让currentDispatch=OperationQueue.current?.underlyingQueue{
打印(当前发送)
//这给了我“
}
LoadingLabel.text=字符串(格式:“%2.0f”,百分比)
ProgressBar.progress=进度
}
请发布一些代码您是否使用dispatch\u async(dispatch\u get\u main\u queue(),^{/*code*/})
更新UI元素?是的,我试图从主队列内部显式调用它,但它没有改变任何内容。事实上,我知道我放在那里的print()-语句只有在加载函数完成后才会调用。请先检查加载函数。如果没有看到updateProgressBar函数并确认您确实得到了结果并进入了循环,那么很难判断
func updateLoadingBar(Progress: Float){
let percentage = Progress * 100
if let currentDispatch = OperationQueue.current?.underlyingQueue {
print(currentDispatch)
// this gives me "<OS_dispatch_queue_main: com.apple.main-thread>"
}
LoadingLabel.text = String(format: "%2.0f", percentage)
ProgressBar.progress = Progress
}