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
490
votes
9 answers

NSOperation vs Grand Central Dispatch

I'm learning about concurrent programming for iOS. So far I've read about NSOperation/NSOperationQueue and GCD. What are the reasons for using NSOperationQueue over GCD and vice versa? Sounds like both GCD and NSOperationQueue abstract away the…
202
votes
10 answers

Wait until swift for loop with asynchronous network requests finishes executing

I would like a for in loop to send off a bunch of network requests to firebase, then pass the data to a new view controller once the the method finishes executing. Here is my code: var datesArray = [String: AnyObject]() for key in locationsArray { …
Josh
  • 2,547
  • 3
  • 13
  • 28
95
votes
17 answers

Get notification when NSOperationQueue finishes all tasks

NSOperationQueue has waitUntilAllOperationsAreFinished, but I don't want to wait synchronously for it. I just want to hide progress indicator in UI when queue finishes. What's the best way to accomplish this? I can't send notifications from my…
Kornel
  • 97,764
  • 37
  • 219
  • 309
83
votes
6 answers

NSOperation and NSOperationQueue working thread vs main thread

I have to carry out a series of download and database write operations in my app. I am using the NSOperation and NSOperationQueue for the same. This is application scenario: Fetch all postcodes from a place. For each postcode fetch all…
Zach
  • 9,989
  • 19
  • 70
  • 107
56
votes
7 answers

How do I use NSOperationQueue with NSURLSession?

I'm trying to build a bulk image downloader, where images can be added to a queue on the fly to be downloaded, and I can find out the progress and when they're done downloading. Through my reading it seems like NSOperationQueue for the queue…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
52
votes
6 answers

Subclassing NSOperation to be concurrent and cancellable

I am unable to find good documentation about how to subclass NSOperation to be concurrent and also to support cancellation. I read the Apple docs, but I am unable to find an "official" example. Here is my source code : @synthesize isExecuting =…
thierryb
  • 3,660
  • 4
  • 42
  • 58
50
votes
4 answers

How to cancel NSBlockOperation

I have a long running loop I want to run in the background with an NSOperation. I'd like to use a block: NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ while(/* not canceled*/){ //do something... } }]; The…
jemmons
  • 18,605
  • 8
  • 55
  • 84
41
votes
7 answers

NSOperation on the iPhone

I've been looking for some concrete scenarios for when NSOperation on the iPhone is an ideal tool to use in an application. To my understanding, this is a wrapper around writing your own threaded code. I haven't seen any Apple demo apps using it,…
Coocoo4Cocoa
  • 48,756
  • 50
  • 150
  • 175
38
votes
5 answers

How To Download Multiple Files Sequentially using NSURLSession downloadTask in Swift

I have an app that has to download multiple large files. I want it to download each file one by one sequentially instead of concurrently. When it runs concurrently the app gets overloaded and crashes. So. Im trying to wrap a downloadTaskWithURL…
CraigH
  • 2,041
  • 3
  • 30
  • 48
37
votes
3 answers

CoreAnimation warning deleted thread with uncommitted CATransaction

I am having issues with the following warning: CoreAnimation: warning, deleted thread with uncommitted CATransaction; set CA_DEBUG_TRANSACTIONS=1 in environment to log backtraces. I am using an NSOperation object to perform some calculations, once…
Milly
  • 675
  • 1
  • 7
  • 12
35
votes
4 answers

Asynchronous methods in NSOperation

I'm fetching some data from Facebook Connect (using the FBConnect Objective-C 2.0 framework) and I'm doing all that in an NSOperation. It is in an NSOperation because I have several other operations that run as well and this is one of them. The…
Michael Waterfall
  • 20,497
  • 27
  • 111
  • 168
32
votes
3 answers

Trying to Understand Asynchronous Operation Subclass

I am trying to get started with using Operations in a side project rather than having closure-based callbacks littered throughout my networking code to help eliminate nested calls. So I was doing some reading on the subject, and I came across this…
Nick Kohrn
  • 5,779
  • 3
  • 29
  • 49
29
votes
4 answers

Which tasks are more suitable to NSOperation than GCD?

Which tasks would be better suited to using NSOperation as opposed to using GCD when programming for the iPhone? To me they seem to do the same thing. I can't see the strengths and weaknesses one has over the other.
dubbeat
  • 7,706
  • 18
  • 70
  • 122
28
votes
3 answers

Default value of maxConcurrentOperationCount for NSOperationQueue

As the title suggests, what is the default value of the maxConcurrentOperationCount for NSOperationQueue? Is it set to a value of 1?
lakshmen
  • 28,346
  • 66
  • 178
  • 276
28
votes
7 answers

NSOperation - Forcing an operation to wait others dynamically

I am trying to implement an operation queue and I have the following scenario: NSOperation A NSOperation B NSOperation C NSOperation D NSOperationQueue queue I start adding A to queue. During the execution of A I need to get some data from B and I…
Rafael
  • 1,655
  • 3
  • 17
  • 25
1
2 3
65 66