1

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.

Apoorv Mote
  • 523
  • 3
  • 25

2 Answers2

0

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)
}
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58
0

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
        })
    })
}
Daniel
  • 3,758
  • 3
  • 22
  • 43
  • Its bit too complicated & other solution was easier. But thanks for the answer. – Apoorv Mote Jun 21 '15 at 05:48
  • It's actually not more complicated at all because it's doing the same exact thing as the code you chose to go with. The only difference is that what I suggested uses the new way of handling threading queues. And is more user friendly if the processing happens very quickly and the progress meter won't shoot away like a lightning without the user realising what that zap was. – Daniel Jun 21 '15 at 11:24