1

I have a question about nested classes and such. I have a UIView that is handling the download of JSON data onto the device, and I have a progress bar which shows the user progress. Since I have to wait for the entire download to finish before going on, I implemented a helper class so I can use the trailing closures (found here)

Any who, here is come code:

In my viewDidLoad

@IBOutlet weak var ProgressBar: UIProgressView!

override func viewDidLoad() {
    super.viewDidLoad()

    ProgressBar.setProgress(0.10, animated: true)

    updateCoreStack.needsCMS = true
    updateCoreStack.needsSOC = true
    updateCoreStack.needsDIN = true
    updateCoreStack.progress = ProgressBar
    updateCoreStack.beginUpdate{ completion in
        //fires when all updates return!
        print(completion)
    }

}

And now here's the helper class:

...
let updateCoreStack = uCoreStack()
class uCoreStack {
    var progress:UIProgressView? = nil

    var needsCMS = false
    var needsDIN = false
    var needsSOC = false

    func beginUpdate(complete: @escaping (String)->()) {
        ...
        self.progress?.setProgress((self.progress?.progress)! + 0.05, animated: true)
        ...
    }
}

It's possibly noteworthy that beginUpdate may be called more than once, ie, per normal use, beginUpdate may be active 3 times. (If that even matters at all...)

With this implementation, I receive the following error per EVERY progressBar change:

2016-11-21 23:50:34.799607 DemoApp[779:241618] This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

Stack:(

0 CoreFoundation 0x000000018505c1d8 + 148

1 libobjc.A.dylib 0x0000000183a9455c objc_exception_throw + 56

2 CoreFoundation 0x000000018505c108 + 0

3 Foundation 0x0000000185c40f0c + 192

4 Foundation 0x0000000185c40c4c + 76

5 Foundation 0x0000000185a8c5dc + 112

6 Foundation 0x0000000185c3f8e8 + 112

7 UIKit 0x000000018aeb5ea0 + 244

8 UIKit 0x000000018aed9d78 + 660

9 UIKit 0x000000018aef0b84 + 108

10 UIKit 0x000000018b3cd7d8 + 276

11 DemoApp 0x0000000100122f70 )

Any thoughts or tips? Thank you in advance.

Bump...

Community
  • 1
  • 1

1 Answers1

0

You seem to be doing work on another thread and then trying to update the status. Any view updates need to happen on the main thread. Here is an example of how to do that: Updating the UI Using Dispatch_Async in Swift

Community
  • 1
  • 1
Patrick Tescher
  • 3,387
  • 1
  • 18
  • 31