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
10
votes
1 answer

AFNetworking - Why does it spawn a network request thread?

I'm trying to understand Operations and Threads better, and looked to AFNetworking's AFURLConnectionOperation subclass for example, real-world, source code. My current understanding is when instances of NSOperation are added to an operation queue,…
edelaney05
  • 6,822
  • 6
  • 41
  • 65
10
votes
2 answers

Why does NSOperation example code uses @try & @catch

In Apple's Concurrency Programming Guide the NSOperation subclass examples (both non-concurrent and concurrent varieties) use exception handling and I'm wondering why they are encouraging this style within operations. Listing 2-4 Responding to a…
edelaney05
  • 6,822
  • 6
  • 41
  • 65
10
votes
1 answer

Creating a custom method with completion block

Possible Duplicate: Implementing a method taking a block to use as callback I couldn't find any clear explanation about how to implement a method that executes a completion block. I know that NSOperation can be subclasses and used with calling…
Kaan Dedeoglu
  • 14,765
  • 5
  • 40
  • 41
9
votes
2 answers

iOS - How to check if an NSOperation is in an NSOperationQueue?

From the docs: An operation object can be in at most one operation queue at a time and this method throws an NSInvalidArgumentException exception if the operation is already in another queue. Similarly, this method throws an…
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
9
votes
3 answers

Generic NSOperation subclass loses NSOperation functionality

Today I've met one weird issue when I was trying to 'generalize' my 'CoreData importing operations'. It appeared that if I create a generic subclass of NSOperation the main() func won't be called. Simple example: class MyOperation:…
Nevs12
  • 599
  • 6
  • 13
9
votes
2 answers

How to cancel UIActivityItemProvider and don't show activity?

I'm using UIActivityItemProvider subclass to provide custom data. But sometimes getting data fails and I don't want to present activity (e.g. message composer). Tried [self cancel] and return nil; in item method, but message composer still shows…
feduza
  • 101
  • 3
9
votes
1 answer

NSOperation is not happening in background thread

I created an NSOperation subclass to handle some zip archive operations. No matter what, if I override -start or -main this block of code always happens: if ([NSThread isMainThread]) { NSLog(@"I am in the main thread"); return; } Any idea…
Daddy
  • 9,045
  • 7
  • 69
  • 98
8
votes
1 answer

Core Data/NSOperation: crash while enumerating through and deleting objects

I have a core data based app that has a one object (a list) to many objects (list items) relationship. I'm working on syncing data between devices, and as part of that I import lists from XML files in background threads (via an NSOperation…
Jim Rhoades
  • 3,310
  • 3
  • 34
  • 50
8
votes
2 answers

2017 / Swift 3.1 - GCD vs NSOperation

I am diving a bit deeper into concurrency and have been reading extensively about GCD and NSOperation. However, a lot of posts like the canonic answer on SO are several years old. It seemed to me that NSOperation main advantages used to be, at the…
Herakleis
  • 513
  • 5
  • 21
8
votes
0 answers

App crashed in Dispatch queue: NSOperationQueue

I sometimes get the following crash in my app: Crashed Thread: 4 Dispatch queue: NSOperationQueue 0x7fc2d96277c0 :: NSOperation 0x7fc2d9704440 (QOS: UTILITY) Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: …
Mugen
  • 8,301
  • 10
  • 62
  • 140
8
votes
1 answer

Using NSProgress with nested NSOperations

I've been investigating NSProgress but have found the existing documentation, class reference and tutorials to be lacking. I'm mainly wondering if my NSProgress is applicable to my use case. The class reference documentation alternatively refers to…
8
votes
4 answers

Naming Threads in an NSOperationQueue

NSOperationQueue creates many threads, as you'd expect, and desire. But when you pause the app and debug it in Xcode, it's unclear which threads belong to one operation queue and which belong to another. I've tried: [NSThread currentThread] setName:…
Nick Locking
  • 2,147
  • 2
  • 26
  • 42
8
votes
1 answer

iOS - Async NSURLConnection inside NSOperation

I know this question was asked many times on SO, but I didn't manage to make it work in my project... So, I want to subclass NSOperation and make it download a file using NSURLConnection. What is the right way to do it? here is my code which…
Oleg
  • 2,984
  • 8
  • 43
  • 71
7
votes
3 answers

How to stop current NSOperation?

I'm using NSOperationQueue, and NSOperation for running some function on background click. But I want to be able, when user clicks some button, stop that Operation. How can I do it? Something like, [currentoperation stop]; Cancel - won't work me.…
User1234
  • 2,362
  • 4
  • 27
  • 57
7
votes
1 answer

Unit Test NSOperation?

I would like to test an NSOperation subclass. I tried to do this in my SenTestCase subclass: - (void)setUp { [super setUp]; _importQueue = [[NSOperationQueue alloc] init]; [_importQueue setMaxConcurrentOperationCount:1]; …
fabian789
  • 8,348
  • 4
  • 45
  • 91