During a loading-process I'm trying to update my UI (a label and a progress bar) from within a loop.
The UI is not updating though, until the loading-function is done.
The loading-function is not using another thread, I'm still in the main thread so from my understanding it should be possible to instantly-update the UI...
Is there something I am missing?
loading-function: (called from viewDidAppear)
func LoadDataFromDataCore(){
//1
guard let appDelegate =
UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext =
appDelegate.persistentContainer.viewContext
//2
let fetchRequest =
NSFetchRequest<NSManagedObject>(entityName: "CDGlobeTile")
//3
do {
let DataCoreResult = try managedContext.fetch(fetchRequest)
var LabelFromCDArray = [LabelTile]()
for (index, CDGlobeTile) in DataCoreResult.enumerated() {
updateLoadingBar(Progress: Float(index) / Float(DataCoreResult.count - 1))
// "unpack" the data from CoreData...
}
do{
try managedContext.save()
} catch let error as NSError {
print("Error saving context. \(error), \(error.userInfo)")
}
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
print(" finished loading")
}
updateLoadingBar - function:
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
}