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

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