develop
OperationQueue 본문
반응형
작업 큐라는 방식이고 병렬프로그래밍을 하기위한 개념이다.
큐에 작업들을 넣으면 알아서 별도의 Thread에서 실행시켜 준다.
let queue = OperationQueue()
queue.addOperation {
(0...3).forEach({ print("queue1: \($0)") })
}
let operation = Operation()
operation.completionBlock = { () in
(0...3).forEach({ print("queue2: \($0)") })
}
queue.addOperation(operation)
queue1: 0
queue2: 0
queue2: 1
queue1: 1
queue2: 2
queue1: 2
queue2: 3
queue1: 3
간단하게는 OperationQueue를 만들고 큐에 Operation을 넣으면 자동으로 Operation이 실행된다.
Operation을 상속받은 BlockOperation클래스도 있다.
let queue = OperationQueue()
let blockOperation = BlockOperation {
(0...3).forEach({ print("block1: \($0)") })
}
blockOperation.addExecutionBlock {
(0...3).forEach({ print("block2: \($0)") })
}
queue.addOperation(blockOperation)
block2: 0
block1: 0
block1: 1
block2: 1
block1: 2
block2: 2
block1: 3
block2: 3
하나 이상의 블록을 동시에 실행할 수 있다.
let queue = OperationQueue()
let blockOperation = BlockOperation()
blockOperation.addExecutionBlock {
sleep(2)
(0...3).forEach({ print("block1: \($0)") })
}
blockOperation.addExecutionBlock {
(0...3).forEach({ print("block2: \($0)") })
}
queue.addOperation(blockOperation)
print("isFinished: \(blockOperation.isFinished)")
sleep(1)
print("isFinished: \(blockOperation.isFinished)")
sleep(3)
print("isFinished: \(blockOperation.isFinished)")
isFinished: false
block2: 0
block2: 1
block2: 2
block2: 3
isFinished: false
block1: 0
block1: 1
block1: 2
block1: 3
isFinished: true
BlockOperation의 모든 블럭들이 끝나야 BlockOperation이 완료된다.
let operationQueue = OperationQueue()
let blockOperation = BlockOperation {
(0...3).forEach({ print("block1: \($0)") })
}
let blockOperation2 = BlockOperation {
(0...3).forEach({ print("block2: \($0)") })
}
blockOperation.addDependency(blockOperation2)
operationQueue.addOperation(blockOperation)
operationQueue.addOperation(blockOperation2)
block2: 0
block2: 1
block2: 2
block2: 3
block1: 0
block1: 1
block1: 2
block1: 3
Operation에 Dependency(종속성)를 줄수 있다.
Queue에 넣지 않고 Operation만 사용할 수 있다.
let operation = Operation()
operation.completionBlock = {
print("operation")
}
operation
let operation = Operation()
operation.completionBlock = {
print("operation")
}
print("isCancelled: \(operation.isCancelled)")
operation.cancel()
print("isCancelled: \(operation.isCancelled)")
if !operation.isCancelled {
operation.start()
}
isCancelled: false
isCancelled: true
cancel메서드를 호출하면 isCancelled프로퍼티의 값이 변한다.
NSOperation클래스는 다음 Key-Path에 대해 Key-Value Observing(KVO)(2021/01/31 - [iOS] - KVC KVO (Key-Value Coding, Key-Value Observing)) 를 준수한다.
- isCancelled
- isAsynchronous
- isExecuting
- isFinished
- isReady
- dependencies
- queuePriority
- completionBlock
더 많은 프로퍼티와 메서드는 공식 홈페이지에서 확인 할 수 있다.
Operation: https://developer.apple.com/documentation/foundation/operation
OperationQueue: https://developer.apple.com/documentation/foundation/operationQueue
반응형
'iOS' 카테고리의 다른 글
UITableView UICollectionView Reorder (0) | 2021.02.07 |
---|---|
GCD (0) | 2021.02.06 |
Thread (0) | 2021.02.04 |
DispatchWorkItem (0) | 2021.02.03 |
LazySequence (0) | 2021.02.02 |
Comments