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

Passing result of NSOperation to another NSOperation

I have two NSOperation that are responsible for Downloading, and Parsing. After the downloading operation is successful and I receive some NSData I want to then set that data as the data to be used by the parsing operation: init(context:…
Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
26
votes
3 answers

NSOperation property overrides (isExecuting / isFinished)

I am subclassing NSOperation in Swift and need to override the isExecuting and isFinished properties since I am overriding the start method. The problem I run into is how to preserve key-value observing (KVO) while also being able to override these…
Erik
  • 12,730
  • 5
  • 36
  • 42
25
votes
1 answer

Is @autoreleasepool still required for modern iOS 8 NSOperation usage?

I’ve read through Concurrency Programming Guide In the guide the text states that GCD dispatch queues define their own @autoreleasepool pools and mentions that it’s still recommended to define one at a per dispatch level, yet for NSOperation…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
21
votes
5 answers

Best practice to send a lot of data in background on iOS4 device?

I have an app that needs to send data (using POST) to a server. This function has to be on one of the NavigationController sub-controllers and user should be able to navigate away from this controller and/or close the app (only iPhone4/iOS4 will be…
cocoapriest
  • 1,869
  • 3
  • 22
  • 36
20
votes
4 answers

Why does NSOperation disable automatic key-value observing?

When working with a custom NSOperation subclass I noticed that the automatic key-value observing is disabled by the [NSOperation automaticallyNotifiesObserversForKey] class method (which returns NO at least for some key paths). Because of that the…
zoul
  • 102,279
  • 44
  • 260
  • 354
20
votes
3 answers

Learning NSBlockOperation

I'm a big fan of blocks, but have not used them for concurrency. After some googling, I pieced together this idea to hide everything I learned in one place. The goal is to execute a block in the background, and when it's finished, execute another…
danh
  • 62,181
  • 10
  • 95
  • 136
19
votes
3 answers

NSOperation wait until asynchronous block executes

i need to put asynchronous operations into an operation queue, however, they need to execute on after the other self.operationQueue = [NSOperationQueue new]; self.operationQueue.maxConcurrentOperationCount = 1; [self.operationQueue…
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
19
votes
4 answers

NSOperation and NSNotificationCenter on the main thread

I have an NSOperation. When it is finished I fire a NSNotificationCenter to let the program know that the NSoperation is finished and to update the gui. To my understanding listeners to the NSNotification will not run on the main thread because the…
Mel
  • 2,055
  • 2
  • 26
  • 45
18
votes
3 answers

How to do two concurrent API calls in swift 4

Thanks in advance for help, I have two API calls, both are concurrent and any call could be success first(I don't want call in sequence), after success of both calls, I have to stop my activity indicator and reload my tableView, Here is my code but…
King
  • 259
  • 1
  • 4
  • 12
18
votes
2 answers

how to cancel out of operation created with addOperationWithBlock?

I'm using NSOperationQueue's addOperationWithBlock. From within the block, how do I check to see if I'm supposed to cancel the operation? Or access any NSOperation properties/methods? [myOperationQueue addOperationWithBlock: ^{ while ( /* long…
TomSwift
  • 39,369
  • 12
  • 121
  • 149
18
votes
3 answers

What is the difference between NSInvocationOperation and NSBlockOperation

There are three operation classes in Foundation Framework(NSOperation, NSInvocationOperation and NSBlockOperation). I already read the concurrency programming guide but I did't understand exactly what is the difference between these three classes.…
MobileDev
  • 1,024
  • 9
  • 21
18
votes
1 answer

How do I do an Asynchronous NSURLConnection inside an NSOperation?

I want to do an Asynchrous NSURLConnection inside of an NSOperation on a background thread. (it is because I'm doing some very expensive operations on the data as they come back that want to be done as the data comes in and in background) Here is my…
Carl Coryell-Martin
  • 3,410
  • 3
  • 26
  • 23
18
votes
3 answers

How can I Pause a NSOperation in a NSOperationQueue?

I need to pause a running NSOperation which was inserted in an NSOperationQueue. Currently I am canceling all operations and restarting them. But this would lead to some kind of duplication in terms of process done. I tried with setSuspended flag of…
Advaith
  • 1,087
  • 3
  • 12
  • 31
17
votes
5 answers

Undoing Core Data insertions that are performed off the main thread

I'm working on some code that uses an NSOperation to import data. I'd like for the user to be able to undo the NSManagedObject instances that are created during the import operation. From what I can tell, it's impossible to use the…
17
votes
3 answers

Xcode tests pass in isolation, fail when run with other tests

I've written some asynchronous unit tests with XCTest expectations to test a networking class I wrote. Most of my tests work every time. There are a few tests that fail when I run the whole suite, but pass on their own. Other tests fail, but…
Moshe
  • 57,511
  • 78
  • 272
  • 425
1
2
3
65 66