Questions tagged [operationqueue]

Operation queue is a traditional Apple technology for asynchronously managing queue of pending tasks, with control over priorities, dependencies, degree of concurrency, cancelation, and allowing one to manage queue of tasks that are, themselves, asynchronous.

An operation queue is the Cocoa equivalent of a concurrent dispatch queue and is implemented by the OperationQueue class. It is built upon the (GCD) framework. It is now largely supplanted by .

A few characteristics of operation queues that distinguish them from GCD dispatch queues:

  • Whereas dispatch queues always execute tasks in first-in, first-out order, operation queues take other factors into account when determining the execution order of tasks. Primary among these factors is whether a given task depends on the completion of other tasks. You configure dependencies when defining your tasks and can use them to create complex execution-order graphs for your tasks.

  • Operation queues can be used to manage dependencies between tasks that are, themselves, asynchronous (e.g., a network request). To wrap an asynchronous task in an Operation requires one to create a custom Operation subclass that performs the necessary KVO for isFinished, etc. See the Operation documentation.

  • Operation queues are useful when attempting to control the degree of concurrency. Dispatch queues are either serial or concurrent, but offer no control over the degree of concurrency.

  • Operation objects offer a richer mechanism to handle cancelation, whereby not only can a computational process periodically check isCancelled, but where relevant, you can override the cancel method (e.g., to proactively cancel an asynchronous task wrapped in a custom Operation subclass).

  • One can establish dependencies between Operation Oobjects.

The tasks you submit to an operation queue can either be a closure, a BlockOperation, or an Operation. An operation object is an object that encapsulates the work you want to perform and any data needed to perform it. Because the Operation class is essentially an abstract base class, you typically define custom subclasses to perform your tasks. However, the Foundation framework does include some concrete subclasses that you can create and use as is to perform tasks.

Operation objects generate key-value observing (KVO) notifications, which can be a useful way of monitoring the progress of your task. Although operation queues always execute operations concurrently, you can use dependencies to ensure they are executed serially when needed.

Nowadays, if trying to schedule asynchronous tasks, one would consider in lieu of an operation queue.

See also

20 questions
5
votes
1 answer

How to use OperationQueue with async functions?

I need to run several functions and the order in which the functions are executed is very important. I have two non-async functions that need to be run and then two async functions that need to be executed after that. All of the functions must be…
devOP1
  • 295
  • 3
  • 13
3
votes
2 answers

Can asynchronous operations be used with `progress` on OperationQueue?

Starting in iOS13, one can monitor the progress of an OperationQueue using the progress property. The documentation states that only operations that do not override start() count when tracking progress. However, asynchronous operations must override…
John Slade
  • 219
  • 1
  • 8
2
votes
1 answer

print 1 to 10 in OperationQueue don't print whole number

I'm studying Combine Scheduler and I have this example code from Raywenderlich book let queue = OperationQueue() let subscription = (1...10).publisher .receive(on: queue) .sink { value in print("Received \(value) on thread…
하닝야
  • 47
  • 5
1
vote
1 answer

Observe URLSession operation queue count?

I am using a URLSession setup this way: public struct NetworkSession { public var session: URLSession public let sessionOperationQueue = OperationQueue() public init() { let sessionConfiguration =…
zumzum
  • 17,984
  • 26
  • 111
  • 172
1
vote
0 answers

Trying to upload Multiple files to AWS using AWSS3TransferUtility in the Background

I have a situation in my App which I need to upload several files ranging from 10-50+- (small size, about 5 mb) and also need to support continuing in the background if needed. At this point I'm able to upload perfectly while the app is in the…
YYfim
  • 1,402
  • 1
  • 9
  • 24
1
vote
1 answer

Subclassing OperationQueue adding sleep period

import Foundation class MyOperationQueue { static let shared = MyOperationQueue() private var queue: OperationQueue init() { self.queue = OperationQueue() queue.name = "com.myqueue.name" …
Zigii Wong
  • 7,766
  • 8
  • 51
  • 79
1
vote
0 answers

Available but Not Available functions on OperationQueue in Swift

MacOS Swift Project, but would for iOS. I am targeting 10.14 I have an OperationQueue. I want to schedule an operation to run later. Easy enough... func schedule(after date: OperationQueue.SchedulerTimeType, interval:…
MongoTheGeek
  • 294
  • 1
  • 10
1
vote
2 answers

Updating UI when working with OperationQueue

I have an operation queue setup as follows: let queue = OperationQueue() queue.name = "com.company.myQueue" queue.qualityOfService = .userInitiated queue.maxConcurrentOperationCount = 64 ... var current = 0 var totalCount = someArray.count for i in…
Joseph
  • 9,171
  • 8
  • 41
  • 67
0
votes
1 answer

How-to: UITextField wait until user stopped typing

I'm trying to find the best way to handle this. I'm currently looking to wait until user stops typing (let's say delay 3s) before running some code. Initially I was going to use textFieldDidEndEditing but as we know this only executes when user…
0
votes
1 answer

Waiting on only one operation in an OperationQueue

When using OperationQueues in Swift, what's the best-practise way to wait until any operation in the queue has completed, as opposed to the usual requirement of waiting until all have completed via waitUntilAllOperationsAreFinished()? The motivation…
davidf2281
  • 1,319
  • 12
  • 20
0
votes
1 answer

Limiting the number of fetch requests in URLSession with SwiftUI?

I'm using an async image loader to fetch images from a URLRequest, and I'm trying to wrap my code inside of an Operation so I can use .maxConcurrentOperationCount for an OperationQueue, because I'm supposed to limit the number of downloads to 3 at a…
0
votes
1 answer

EXC_BAD_ACCESS KERN_INVALID_ADDRESS crash in addOperation of OperationQueue

I have asynchronous operation implementation like this: class AsyncOperation: Operation { private enum State: String { case ready, executing, finished fileprivate var keyPath: String { return "is\(rawValue.capitalized)" } } …
Shohin
  • 519
  • 8
  • 11
0
votes
1 answer

How to interrupt Thread.sleep. Alternatives?

I have implemented an Operation on an OperationQueue. override func main() { super.main() if isCancelled { return } if member.memberType == .timed { triggerRestEvent(duration: member.restDuration) } if…
bobby123uk
  • 892
  • 4
  • 17
0
votes
0 answers

OperationQueue - crash when editing the same array from multiple operations

I have an OperationQueue with multiple custom Operations which all append to the same array on completion (each operation downloads a file from user's iCloud and when it's done it appends the file to the array) This, sometimes, causes the app to…
SmartTree
  • 1,381
  • 3
  • 21
  • 40
0
votes
1 answer

OperationQueue with custom `maxConcurrentOperationCount` does not pick up / execute all operations in the queue, after finishing first operation

I'm sure there's something wrong with my logic, just cant figure out what it is. There's a "Service" class, which has an operation queue: class Service { let queue: OperationQueue = { var queue = OperationQueue() queue.name =…
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
1
2