Can you use cancel/isCancelled with GCD/dispatch_async?

GCD does not have built-in support for cancellation; if it’s important to be able to cancel your background task, then checking a flag like you’ve demonstrated is an acceptable solution. However, you may want to evaluate how quickly the cancellation needs to respond; if some of those methods calls are fairly short, you may be … Read more

Generic NSOperation subclass loses NSOperation functionality

Workaround: You can create NSOperation subclass (no generic), override main and call you own ‘execute’ func, which can be overriden by generic subclasses. Example: class SwiftOperation : NSOperation { final override func main() { execute() } func execute() { } } class MyOperation<T> : SwiftOperation { override func execute() { println(“My operation main was called”) … Read more

How to cancel NSBlockOperation

Doh. Dear future googlers: of course operation is nil when copied by the block, but it doesn’t have to be copied. It can be qualified with __block like so: //THIS MIGHT LEAK! See the update below. __block NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while( ! [operation isCancelled]){ //do something… } }]; UPDATE: Upon further meditation, it … Read more

Subclassing NSOperation to be concurrent and cancellable

Okay, so as I understand it, you have two questions: Do you need the performSelectorOnMainThread: segment that appears in comments in your code? What does that code do? Why is the _isCancelled flag is not modified when you call cancelAllOperations on the NSOperationQueue that contains this operation? Let’s deal with these in order. I’m going … Read more

NSOperation and NSOperationQueue working thread vs main thread

My question is whether the database write operation occur in main thread or in a background thread? If you create an NSOperationQueue from scratch as in: NSOperationQueue *myQueue = [[NSOperationQueue alloc] init]; It will be in a background thread: Operation queues usually provide the threads used to run their operations. In OS X v10.6 and … Read more

Get notification when NSOperationQueue finishes all tasks

Use KVO to observe the operations property of your queue, then you can tell if your queue has completed by checking for [queue.operations count] == 0. Somewhere in the file you’re doing the KVO in, declare a context for KVO like this (more info): static NSString *kQueueOperationsChanged = @”kQueueOperationsChanged”; When you setup your queue, do … Read more

How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift

Your code won’t work because URLSessionDownloadTask runs asynchronously. Thus the BlockOperation completes before the download is done and therefore while the operations fire off sequentially, the download tasks will continue asynchronously and in parallel. While there are work-arounds one can contemplate (e.g., recursive patterns initiating one request after the prior one finishes, non-zero semaphore pattern … Read more

NSOperation vs Grand Central Dispatch

GCD is a low-level C-based API that enables very simple use of a task-based concurrency model. NSOperation and NSOperationQueue are Objective-C classes that do a similar thing. NSOperation was introduced first, but as of 10.5 and iOS 2, NSOperationQueue and friends are internally implemented using GCD. In general, you should use the highest level of … Read more