Search code examples
iosswiftanimationcloudkituiprogressview

Hide UIProgressView when animation is completed


I am saving array of records in cloud kit with CKOperation as follows & displaying progress with progress view.

    let saveOperation = CKModifyRecordsOperation(recordsToSave: ckRecord, recordIDsToDelete: nil)

    saveOperation.perRecordProgressBlock = {
        record, progress in
        if progress >= 1 {
            self.completedRecord.addObject(record)
            let totalSavedRecord = Double(self.completedRecord.count)
            let totalProgress = totalSavedRecord / self.totalStudent
            let percentageProgress = Float(totalProgress * 100)
            progressView.setProgress(percentageProgress, animated: true)

            println(progress)
            println(progressView.progress)
            println(percentageProgress)

        }
    }

I want to hide progress view when it reaches 100% & animation is completed.

Currently I reach percentageProgress to 100.0 quickly but progress view animation happens with some delay. If I hide when percentageProgress reaches 100.0 then I will never see any animation.

Value of progress is 1.0 throughout.

Value of progressView.progress is also 1.0 throughout.

Again I want to show the complete animation from 0% to 100% & only then hide the progress view.


Solution

  • CloudKit callback blocks are executed on a background thread. When you update your UI you should do that on the main thread. Otherwise you could see strange delays like this. Try wrapping your code in a block like this:

    NSOperationQueue.mainQueue().addOperationWithBlock {
       progressView.setProgress(percentageProgress, animated: true)
    }