카테고리 | 게시글 작성 날짜 | 게시글 최근 수정 날짜 | 작성자 |
iOS | 2021.12.01. 21:47:11 | 2022.07.20. 21:43:11 | Dev.Yang |
Operation Queue는 연산(Operation)의 실행을 관리하며 연산의 대기열을 관리하는 작업을 수행합니다. 연산의 대기열을 관리하는 작업으로는 연산(Operation) 작업들간의 종속성을 추가, 재사용, 일시정지, 삭제 작업을 수행 할 수 있습니다. 또한, 연산(Operation)은 작업이 끝날 때까지 대기열에 남아 있으며 연산(Operation)을 대기열에서 제거하는 방법은 연산(Operation)을 취소하는 방법입니다.
연산(Operation)을 대기열에서 취소하는 방법은 아래와 같습니다.
- 연산 객체(Operation Object)의 cancel() 메서드를 호출하는 방법
- Oeration Queue의 cancelAllOperations() 메서드를 호출하여 대기열에 있는 모든 연산(Operation)을 취소하는 방법
추가적으로 실행 중인 연산(Operation)의 경우 연산 객체(Operation Object)의 취소 상태를 확인하고 실행 중인 연산(Operation)을 중지하고 완료 상태로 변경됩니다.
- Operation은 Task와 관련된 코드와 데이터를 나타내는 추상 클래스입니다.
- 연산 객체 (Operation Object)는 애플리케이션에서 수행하려는 연산(Operation)을 캡슐화하는 데 사용하는 Foundation 프레임 워크의 Operation 클래스 인스턴스입니다.
- 대기열(Queue)에 추가한 동작은 직접 제거할 수 없습니다.
또한, OperationQueue는 4가지의 상태가 존재하며 Ready (준비), Execute (실행), Cancel (취소), Finish (완료) 형태가 존재합니다.
🛠 특정 Operation Queues 가져오기 (Accessing Specific Operation Queues)
- class var main: OperationQueue - 메인 스레드와 관련 된 Operation Queue를 반환합니다.
- class var current: OperationQueue? - 현재 작업이 시작 된 Operation Queue를 반환합니다.
🛠 대기열에서 작업 관리 (Managing Operations in the Queue)
- func addOperation(Operation) - 연산 객체(Operation Object)를 대기열(Queue)에 추가합니다.
- func addOperations([Operation], waitUntilFinished: Bool) - 연산 객체(Operation Object) 배열을 대기열(Queue)에 추가합니다.
- func addOperation(() -> Void) - 전달한 클로저를 연산 객체(Operation Object)에 감싸서 대기열(Queue)에 추가합니다.
- func cancelAllOperations() - 대기 중이거나 실행 중인 모든 연산(Operation)을 취소합니다.
- func waitUntilAllOperationsAreFinished() - 대기 중인 모든 연산(Operation)과 실행 중인 연산(Operation)이 모두 완료될 때까지 현재 스레드로의 접근을 차단합니다.
🛠 연산 (Operation)의 실행 관리 (Managing the Execution of Operations)
- var qualityOfService: QualityOfService - 연산 (Operation)에 대하여 QoS 옵션을 설정합니다.
- var maxConcurrentOperationCount: Int - 동시에 실행할 수 있는 연산(Operation)의 최대 수를 설정합니다.
🛠 연산(Operation) 중단 (Suspending Execution)
- var isSuspended: Bool - 대기열(Queue)의 연산(Operation) 작업에 대한 활성화 여부를 나타내는 값입니다. false인 경우 대기열(Queue)에 있는 연산(Operation)을 실행하고, true인 경우 대기열(Queue)에 대기 중인 연산(Operation)을 실행하진 않지만 이미 실행 중인 연산(Operation)은 계속 실행됩니다.
🛠 Asynchronous OperationQueue
비동기적 OperationQueue 작업을 수행하기 위해서 사용되는 기본적인 형태로 상속을 받아서 구현하고자 하는 기능을 구현합니다.
아래의 소스코드는 "iOS Concurrency(동시성) 프로그래밍, 동기 비동기 처리 그리고 GCD/Operation - 디스패치큐와 오퍼레이션큐의 이해" 강의에서 가져왔습니다.
import Foundation
class AsyncOperation: Operation {
enum State: String {
case ready, executing, finished
// KVO notifications을 위한 keyPath설정
fileprivate var keyPath: String {
return "is\(rawValue.capitalized)"
}
}
// 직접 관리하기 위한 상태 변수 생성
var state = State.ready {
willSet {
willChangeValue(forKey: newValue.keyPath)
willChangeValue(forKey: state.keyPath)
}
didSet {
didChangeValue(forKey: oldValue.keyPath)
didChangeValue(forKey: state.keyPath)
}
}
override var isReady: Bool {
return super.isReady && state == .ready
}
override var isExecuting: Bool {
return state == .executing
}
override var isFinished: Bool {
return state == .finished
}
override var isAsynchronous: Bool {
return true
}
override func start() {
if isCancelled {
state = .finished
return
}
main()
state = .executing
}
override func cancel() {
super.cancel()
state = .finished
}
}
🚀 REFERENCE
다 함께 배우고 성장하는 부스트코스
부스트코스(boostcourse)는 모두 함께 배우고 성장하는 비영리 SW 온라인 플랫폼입니다.
www.boostcourse.org
Apple Developer Documentation
developer.apple.com
Operation and OperationQueue Tutorial in Swift
In this tutorial, you will create an app that uses concurrent operations to provide a responsive interface for users by using Operation and OperationQueue.
www.raywenderlich.com
iOS Concurrency(동시성) 프로그래밍, 동기 비동기 처리 그리고 GCD/Operation - 디스패치큐와 오퍼레이션
동시성(Concurrency)프로그래밍 - iOS프로그래밍에서 필요한 동기, 비동기의 개념 및 그를 확장한 GCD 및 Operation에 관한 모든 내용을 다룹니다., - 강의 소개 | 인프런...
www.inflearn.com
Advanced NSOperations | WWDC NOTES
Advanced NSOperations Description: Operations are a flexible way to model your app's business logic, but they can do so much more. See how NSOperation forms the heart of the WWDC app, and how using features like dependencies, readiness, and composition all
www.wwdcnotes.com
'# 애플 [Apple] > iOS' 카테고리의 다른 글
[iOS] Kakao 로컬(Local) API를 사용하여 좌표로 주소 변환하기 (0) | 2022.01.01 |
---|---|
[iOS] 픽셀 (Pixel)과 포인트 (Point) (0) | 2021.12.05 |
[iOS] Apple 푸쉬 알림 서비스 (APNs, Apple Push Notification service) (0) | 2021.11.16 |
[iOS] 애플리케이션 생명 주기 (Application Life Cycle) (0) | 2021.11.04 |
[iOS] 뷰 컨트롤러 (View Controller) (0) | 2021.10.29 |
댓글