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
7
votes
2 answers

Can you use cancel/isCancelled with GCD/dispatch_async?

I've been wondering, can you use cancel/cancelAllOperations/.isCancelled with a thread you have launched with GCD? Currently, I just use a boolean as a flag, to cancel the background process. Let's say you want to do a lot of processing in the…
Fattie
  • 27,874
  • 70
  • 431
  • 719
7
votes
2 answers

How to assure that operations in an OperationQueue are finished one after another

When performing operations that are dependent on each other OperationQueue could be used to assure they are executed in the correct order. However, would it also be possible to assure that operations are finished one after another? Let's assume a…
Taco
  • 701
  • 7
  • 21
7
votes
1 answer

Race condition in Apple's sample code for Advanced NSOperations

TL:DR When is the .Completed state set on NSURLSessionTask and how does it depend/affect the completionHandler of the same task? Is there a way to make sure that the .Completed state is only set after the completionHandler is finished…
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
7
votes
1 answer

Pause NSOperation

I have NSOperationQueue with some NSOperations in it (NSInvocationOperations, in particular). This operations do some calculations and change states of UI elements accordingly (of course, via performSelectorOnMainThread:...), often with use of…
kpower
  • 3,871
  • 4
  • 42
  • 62
7
votes
1 answer

NSOperation Queue behaving abnormally

I have a task of uploading multiple images to the server one by one. So I am using the batch operation process for this. Every time I start the upload procedure, some operations specially the first one completes as soon as it starts and the image…
7
votes
0 answers

Rapid UI updates from a background thread. Best practices

I have a serial NSOperationQueue I have an NSOperation subclass which read a file from disk, create a UIImage from it, and then display the image on the main thread. Pretending the queue containing about a hundred of NSOperations, what would be the…
7
votes
3 answers

Implementing tasks that can be canceled in Bolts Framework (BFTask)

BFTask has been good to me but I have one complaint: I've yet to see a working example of how you ought to cancel a task. The entirety of the documentation on the subject is found on their GitHub page with a single lowly section that includes…
tacos_tacos_tacos
  • 10,277
  • 11
  • 73
  • 126
7
votes
3 answers

Call operation in completion block without memory leak

I am creating an iOS app using swift. Let's say I want to sort an array in a different thread that the main thread. I can subclass NSOperation like that : import UIKit class Operation: NSOperation { var array:[Int] init(array:[Int]){ …
soling
  • 541
  • 6
  • 20
7
votes
2 answers

Calling -(void) cancelAllOperations on NSoperationQueue is not setting the isCancelled property of NSOperation that is present inside the Queue

I am facing problem related to NSoperationQueue. In my code in : -(void) viewDidLoad { //Initialisation of queue and operation. //adding operation to queue [self.operationQueue addOperation:op]; } -(void)…
kidsid49
  • 1,358
  • 3
  • 18
  • 37
7
votes
2 answers

C++ equivalent for NSOperation and NSOperationQueue

Please advise me to how to achieve NSOperation and NSOperationQueue functionality in C++.
user1908860
  • 509
  • 1
  • 9
  • 19
7
votes
3 answers

Cocoa - Return information from NSOperation

I have an IPhone app that uses webservices to get data from a server. I'm putting each call to the webservice in a NSOperation subclass so that it can be threaded. My question is, what is the recommended way to pass back information from a…
Brian
  • 3,571
  • 7
  • 44
  • 70
7
votes
5 answers

iphone - Should I use NSOperationQueue and NSOperation instead of NSThread?

I am facing a design problem of my app. Basically, the followings are what I am going to do in my app. A single task is like this: Read custom object from the underlying CoreData databse Download a json from a url Parse the json to update the…
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
7
votes
4 answers

Waiting for completion block to complete in an AFNetworking request

I am making a JSON request with AFNetworking and then call [operation waitUntilFinished] to wait on the operation and the success or failure blocks. But, it seems to fall right though - in terms of the log messages, I get "0", "3", "1" instead of…
Kamran
  • 843
  • 1
  • 8
  • 19
6
votes
4 answers

NSOperation inside NSOperationQueue not being executed

I really need help here. I'm desperate at this point. I have NSOperation that when added to the NSOperationQueue is not being triggered. I added some logging to see the NSOperation status and this is the result: Queue operations count = 1 Queue…
Martin Garcia
  • 345
  • 5
  • 14
6
votes
2 answers

Using NSThread sleep in an NSOperation

Working with some code, I'm coming across run loops, which I'm new to, inside NSOperations. The NSOperations are busy downloading data - and whilst they are busy, there is code to wait for the downloads to complete, in the form of NSRunLoops and…
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112