Hide UIProgressView when animation is completed

2.2k views Asked by At

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.

2

There are 2 answers

0
Edwin Vermeer On BEST ANSWER

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)
}
2
Daniel On

This worked for me. You just need to call this function whenever your processing is done.

func resetProgressView() {
    let TIME_DELAY_BEFORE_HIDING_PROGRESS_VIEW: UInt32 = 2
    // Wait for couple of seconds so that user can see that the progress view has finished and then hide.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
        sleep(TIME_DELAY_BEFORE_HIDING_PROGRESS_VIEW)
        dispatch_async(dispatch_get_main_queue(), {
        self.progressView.setProgress(0, animated: false)     // set the progress view to 0
        })
    })
}