Questions tagged [nsoperation]

The NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task

The NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task. Because it is abstract, you do not use this class directly but instead subclass or use one of the system-defined subclasses (NSInvocationOperation or NSBlockOperation) to perform the actual task. Despite being abstract, the base implementation of NSOperation does include significant logic to coordinate the safe execution of your task. The presence of this built-in logic allows you to focus on the actual implementation of your task, rather than on the glue code needed to ensure it works correctly with other system objects.

An operation object is a single-shot object—that is, it executes its task once and cannot be used to execute it again. You typically execute operations by adding them to an operation queue (an instance of the NSOperationQueue class). An operation queue executes its operations either directly, by running them on secondary threads, or indirectly using the libdispatch library (also known as Grand Central Dispatch). For more information about how queues execute operations, see NSOperationQueue Class Reference.

If you do not want to use an operation queue, you can execute an operation yourself by calling its start method directly from your code. Executing operations manually does put more of a burden on your code, because starting an operation that is not in the ready state triggers an exception. The isReady method reports on the operation’s readiness.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html

983 questions
5
votes
3 answers

iOS: What happens if I execute a background job and then leave the view controller?

Let's say I have view controller A and view controller B. In VC A, I push VC B. Then in VC B, I execute some background tasks using NSOperation. In the background tasks, I modify VC B's variables. What happens if the background tasks are not…
Van Du Tran
  • 6,736
  • 11
  • 45
  • 55
5
votes
1 answer

Is it safe to enumerate through [NSOperationQueue operations]?

Is it safe to enumerate, via fast enumeration, through [NSOperationQueue operations]? Like so: for (NSOperation *op in [operationQueue operations]) { // Do something with op } Since operations are asynchronous and executed on another thread,…
Adam Ernst
  • 52,440
  • 18
  • 59
  • 71
5
votes
2 answers

NSOperation Cancellation: NSInvocationOperation or NSOperation subclass?

I have a fairly simple, though expensive, task that I need to run in the background, which is the standard NSOperation situation. I also need to make sure the operation supports cancellation, and stops appropriately. Given that requirement, which is…
Matt Wilding
  • 20,115
  • 3
  • 67
  • 95
5
votes
3 answers

NSAutoreleasePool. When is it appropriate to create a new autorelease pool?

On iOS/CocoaTouch I often see code that creates a new instance of NSAutoreleasePool within a method. I recently saw one within an NSOperation. What are the ground rules for setting up a new instance of NSAutoreleasePool? Why is this preferable to…
dugla
  • 12,774
  • 26
  • 88
  • 136
5
votes
4 answers

Core Data and NSOperation

I'm currently working with an NSPersistentDocument subclass that uses NSOperation to import data in the background. As per the documentation, I'm observing the NSManagedObjectContextDidSaveNotification after saving in the background task and…
chockenberry
  • 7,811
  • 5
  • 32
  • 41
5
votes
1 answer

Operation went isFinished=YES without being started by the queue it is in

Overview There is an asynchronous operation subclass Added this operation to the queue. I cancelled this operation before it starts. Runtime Error / Warning: SomeOperation went isFinished=YES without being started by the queue it is…
user1046037
  • 16,755
  • 12
  • 92
  • 138
5
votes
1 answer

Overriding default values with operation class in Swift 3

I am attempting to override some values with the Operation class in Swift 3 as part of a conversion process from 2.2, but am encountering an issue with overriding class properties. This is a simplified version of the code that works properly in…
CodeBender
  • 35,668
  • 12
  • 125
  • 132
5
votes
1 answer

Setting main queue as underlying queue for NSOperationQueue

NSOperationQueue class has an underlyingQueue property which is used to set the dispatch queue on which NSOperation instances will be executed. let dispatchQueue = dispatch_queue_create("custom.queue", DISPATCH_QUEUE_SERIAL) let opQueue =…
Said Sikira
  • 4,482
  • 29
  • 42
5
votes
0 answers

Crash at NSOperationQueue addOperation Custom NSOperation

I have a NSOperationQueue, maxConcurrentOperationCount = 1, and Custom NSOperation has a property is block, when the NSOperationQueue addOperation, the app crash, in iOS 9.x, the NSOperation is not nil. following is Apple crash report, but i can't…
fyxrhyry
  • 135
  • 1
  • 8
5
votes
1 answer

NSURLConnection in NSOperation

I am writing a code that reads data from a http connection and stores in a byte array. I have written my own NSOperation class. Reference of the code is Concurrent Operations Demystified My HttpWorker class declaration is like @interface HttpWorker…
Aqueel
  • 1,246
  • 3
  • 23
  • 46
5
votes
2 answers

Get specific NSOperation from NSOperationQueue

Hi there, In my app, I create NSOperations and I add them to a NSOperationQueue. Sometimes, I want to cancel some specific operations from my operation queue, that's why I have defined an identifier property for my NSOperation subclass : @property…
Randy
  • 4,335
  • 3
  • 30
  • 64
5
votes
1 answer

How to Cancel download in Queue using Swift

I have an app where user can download multiple files, sequentially. I have followed Mr. Rob's solution for the sequential downloads. However, I have problem when I try to cancel the download. There are two situation when I try to cancel the…
Syafiq Mastor
  • 178
  • 2
  • 10
5
votes
3 answers

NSOperations, dependencies and failed operations

I've started working with CloudKit and finally started using subclassed NSOperation for most of my async stuff. How ever, I have two questions. How can I mark an operation as failed? That is, if operation A fails, I don't wan't its dependent…
Johan Nordberg
  • 3,621
  • 4
  • 33
  • 58
5
votes
1 answer

perform simultaneous operations based on the some conditions in swift

I have to perform set of operations based on some conditions like fetching data from database which takes approximately 10 seconds per query (ConnectedDevices.getAllDetails() takes 10 to execute and return result). this may be similar to below…
cybergeeeek
  • 410
  • 8
  • 22
5
votes
3 answers

How to subclass NSOperation in Swift to queue SKAction objects for serial execution?

Rob provided a great Objective-C solution for subclassing NSOperation to achieve a serial queuing mechanism for SKAction objects. I implemented this successfully in my own Swift project. import SpriteKit class ActionOperation : NSOperation { …
Kevin Owens
  • 538
  • 5
  • 12