1

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


}
Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • please post some code – Itamar Manor Sep 16 '19 at 13:57
  • are you using `dispatch_async(dispatch_get_main_queue(), ^{ /* code */ }) ` for updating UI elements? – Aleksey Potapov Sep 16 '19 at 14:00
  • yeah, I tried to call it explicitly from inside the main_queue but it's not changing anything. In fact I recognized that print()-statements that I place there are only called after the loading-function is done. – LostReminder Sep 16 '19 at 14:01
  • check your loading function first. Or place it here so we could help. – Aleksey Potapov Sep 16 '19 at 14:02
  • btw check this answer https://stackoverflow.com/a/17920357/1040347 – Aleksey Potapov Sep 16 '19 at 14:03
  • Okay, I posted the code of the loading-function. – LostReminder Sep 16 '19 at 14:07
  • @AlekseyPotapov hmm I see, so am I supposed to do the loading and unpacking in a secondary thread and the UI-updating in the main-thread? I can do that, but I just don't understand why it's not working when I do everything inside the main-thread... – LostReminder Sep 16 '19 at 14:10
  • 1
    hard to tell without seeing the updateProgressBar function and confirming you are definitely getting results and entering the loop – Scriptable Sep 16 '19 at 14:24
  • @LostReminder yes, heavy-duty tasks like fetching/writing data, downloading from the internet should be performed on the background thread. – Aleksey Potapov Sep 16 '19 at 14:47
  • @Scriptable I am entering the loop, I was using print statements to make sure of that. The data is loaded properly – LostReminder Sep 16 '19 at 14:51
  • Instead of ProgressBar.progress try, progressBar.setProgress(progress, animated: true). Also, try calling "updateLoadingBar" on the main que. "DispatchQueue.main.async { updateLoadingBar(...... " – Tofu Warrior Sep 17 '19 at 15:29

0 Answers0