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
11
votes
3 answers

How to properly deal with a deallocated delegate of a queued nsoperation

In my current project, several view controllers (like vc) spawn NSOperation objects (like operation) that are executed on a static NSOperationQueue. While the operation is waiting or running, it will report back to the view controller via delegation…
epologee
  • 11,229
  • 11
  • 68
  • 104
11
votes
1 answer

Properly dealloc NSOperationQueue

I'd like to know what is the proper way to dealloc an ivar NSOperationQueue in case it has still some operations running, which can typically occur when the user suddenly quits the app. In some examples I saw the waitUntilAllOperationsAreFinished…
Tomas Camin
  • 9,996
  • 2
  • 43
  • 62
11
votes
2 answers

How do I cancel an NSOperation's dependencies?

I have some NSOperations in a dependency graph: NSOperation *op1 = ...; NSOperation *op2 = ...; [op2 addDependency:op1]; Here's how I'm running them: NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperation:op1]; [queue…
Matt Fenwick
  • 48,199
  • 22
  • 128
  • 192
11
votes
3 answers

How to communicate results between NSOperation dependencies?

The new Cloud Kit framework uses NSOperation extensively for it's CRUD. The results of those operations are returned in blocks. For example: let fetchOperation = CKFetchRecordsOperation(recordIDs: [recordID1,…
Ward Bekker
  • 6,316
  • 9
  • 38
  • 61
11
votes
3 answers

With NSOperationQueue, how do you add to a background queue instead of main, and how does controlling amount of operations work?

I'm loving NSOperationQueue but I'm having some issues understanding some portions of it. In the second issue of objc.io they go over NSOperationQueue and mention that it has two kinds of queues, the main queue which runs on the main thread, and the…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
11
votes
2 answers

Why my completionBlock never gets called in an NSOperation?

I've sublcassed an NSOperation and set my completionBlock but it seems to never enter even when the operation finishes. Here's my code: A catalog controller class sets up the NSOperation: - (void)setupOperation { ... ImportWordOperation…
carlos_ms
  • 838
  • 9
  • 15
11
votes
1 answer

What's the equivalent of NSOperationQueue in java android?

I love nsoperationqueue in iOS. What's the equivalent in java?
user4951
  • 32,206
  • 53
  • 172
  • 282
10
votes
3 answers

Recommendations to test API request layer in iOS apps using NSOperations and Coredata

I develop an iOS app that uses a REST API. The iOS app requests data in worker threads and stores the parsed results in core data. All views use core data to visualize the information. The REST API changes rapidly and I have no real control over the…
Lars Schneider
  • 5,530
  • 4
  • 33
  • 58
10
votes
2 answers

How to use NSOperation & NSOperationQueue in Objective-C?

I am developing a custom camera application.What I am doing is , I am taking picture using camera and showing them in same VC bottom of the screen. I am storing the image in local dictionaries and NSDocument directory path. If the picture is in…
kavi
  • 143
  • 1
  • 1
  • 9
10
votes
1 answer

Why is my app crashing when I modify a Core Data relationship in an NSOperation subclass?

Background I've got the following tree of objects: Name Project Users nil John nil Documents nil Acme Project …
John Gallagher
  • 6,208
  • 6
  • 40
  • 75
10
votes
3 answers

iOS App Architecture with NSOperations

Two month ago I started to write a new iPhone Application and for this reason I created a generic RESTFul web service, which allows me to have a lot of these necessary features like user authentication, user profiles, a friendship system, media…
Henrik P. Hessel
  • 36,243
  • 17
  • 80
  • 100
10
votes
0 answers

Does Alamofire support NSOperation queue?

I want to implement a batch of request using alamofire. I want to use NSOperation queue to manage it. Is it supported using Alamofire framework.
kalim sayyad
  • 1,076
  • 1
  • 9
  • 13
10
votes
2 answers

Application sticks on OSSpinLockLockSlow

Update 2: I found a workaround which is to synchronize MOC deallocating and saving. Please see the updated project. https://github.com/shuningzhou/MOCDeadLock.git Note: I made it fail more aggressively. Don't run it on a real device! Update: A…
Peter Zhou
  • 3,881
  • 2
  • 21
  • 19
10
votes
1 answer

Solve reader-writer issues with NSOperationQueue?

I know it's possible to solve reader-writer issues in GCD using barriers. Since I (generally) try to use NSOperationQueue instead of GCD when performance isn't a key issue, I'd like an NSOperation-compatible solution to this issue. I've tried to…
10
votes
3 answers

Proper way to deal with cell reuse with background threads?

I have a UICollectionView, but the same methods should apply to UITableViews. Each of my cells contains an image I load from disk, which is a slow operation. To mitigate this, I use an async dispatch queue. This works fine, but quickly scrolling…
akaru
  • 6,299
  • 9
  • 63
  • 102