iOS Concurrency with GCD and Operations - Part 9: | Ray Wenderlich

In this iOS concurrency video tutorial, you'll learn how to perform a cancellation of an Operation.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3648-ios-concurrency-with-gcd-and-operations/lessons/9

Hello, Audrey!

Aren’t we supposed to call super.cancel() in overridden method cancel() in AsyncOperation? Will checks for isCancelled inside main() succeed, because in our cancel implementation we just set operation state to finished, nothing reflect changes in isCancelled property.

P.S. Thanks for great series!

thanks Alexey, you’re absolutely right! In AsyncOperation.swift, the cancel() method should be:

override func cancel() {
  super.cancel()
  state = .Finished
}

thanks again!

Thank Audrey for your tutorial :slight_smile: .
I have a question: In 09_ChallengeFinished
why do you use more OperationQueue

"
let queue = OperationQueue()
let appendQueue = OperationQueue()

…

func generateOperations(_ numberArray: [(Int, Int)]) {
for pair in numberArray {
let operation = SumOperation(input: pair)
operation.completionBlock = {
// DONE: Implement this
// NOTE: Be sure to append to the array on the appendQueue.
guard let result = operation.output else { return }
self.appendQueue.addOperation {
self.outputArray.append((pair.0, pair.1, result))
}
}
queue.addOperation(operation)
}
}
…
"

If i change “self.appendQueue.addOperation” —> “self.queue.addOperation”.
Does it work well?
Sorry for my english is not good :).
thanks again!

hi Vi Tuan: appendQueue is a serial queue; you use this to append each operation to the array, to make sure the pairs are added in the correct order. It doesn’t make any difference with sums, but might be important for real-world operations.

1 Like

Thank you for great tutorial, Audrey. For anyone that has problems with bad exc/ access when using cancellAllOperations(), try commenting out lines

   /*override func cancel() {
         state = .Finished
    }*/

In my case, i was scheduling hundreds of operations for updating calendar views. When I changed view, I canceled pending operations for previous views with cancellAllOperations(). I think the problem was beause it caused state = .finished to be called before it even started, which operation didn’t allow.

1 Like

Attaching a Swift 5 Xcode 11 version of exercise files for this lecture, could probably help those who don’t have Xcode 10 to be able to run the demo. Did nothing but used the default migration tool, and can verify that the end product works just fine (albeit with 2-3 warnings)
Won’t mind if this is made downloadable directly from the lecture video page - http://f.cl.ly/items/2Y0R3U1Z3L0b1w352d3I/09_CancellingTasks.zip

1 Like

@akashlal Please check out the updated version of the course when you get a chance:

https://www.raywenderlich.com/9461083-ios-concurrency-with-gcd-and-operations

I hope it helps!

1 Like