Asynchronous methods in NSOperation

Below is a full example. In your subclass, after your async method completes, call [self completeOperation] to transition to the finished state. @interface AsynchronousOperation() // ‘executing’ and ‘finished’ exist in NSOperation, but are readonly @property (atomic, assign) BOOL _executing; @property (atomic, assign) BOOL _finished; @end @implementation AsynchronousOperation – (void) start; { if ([self isCancelled]) { … Read more

CoreAnimation warning deleted thread with uncommitted CATransaction

In keeping with standard Cocoa paradigms, the recommended solution here is to perform your Core Animation work on the main thread, easily done with GCD: dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate redrawSomething]; }); In general it’s poor form to call objects in contexts they don’t expect, so a good rule of thumb is to always dispatch onto the … 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

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

How do I use NSOperationQueue with NSURLSession?

Your intuition here is correct. If issuing many requests, having an NSOperationQueue with maxConcurrentOperationCount of 4 or 5 can be very useful. In the absence of that, if you issue many requests (say, 50 large images), you can suffer timeout problems when working on a slow network connection (e.g. some cellular connections). Operation queues have … 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