NSOperationQueue

Wongbing·2022년 10월 27일
0

Apple Docs

목록 보기
8/11
post-thumbnail
tags: 문서스터디

Apple Docs - NSOperationQueue

NSOpertaionQueue

A queue that regulates the execution of operations

오퍼레이션의 실행을 통제하는 큐

Declaration

@interface NSOperationQueue: NSObject

Overview

An operation queue invokes its queued NSOperation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it.

오퍼레이션 큐는 우선순위와 준비정도에 따라 대기열에 배치된 NSOperation 객체 를 호출합니다. 당신이 오퍼레이션을 큐에 넣은 이후로는, 오퍼레이션이 일을 마칠때 까지 큐에 남아있습니다. 당신이 추가한 이후로는 큐에서 직접 오퍼레이션을 제거할 수 없습니다.

Note:
Operation queues retain operations until the operations finish, and queues themselves are retained until all operations are finished. Suspending an operation queue with operations that aren't finished can result in a memory leak

오퍼레이션 큐는 오퍼레이션이 끝날 때 까지 오퍼레이션을 유지하고, 큐들은 스스로 모든 오퍼레이션들이 끝날 때 까지 유지된다. 끝나지 않은 오퍼레이션을 가진 오퍼레이션 큐를 중단시키는 것은 결과적으로 메모리 누수를 발생시킵니다.

For more information about using operation queues, see the Concurrency Programming Guide.

Determine the Execution Order

An operation queue organizes and invokes its operations according to their readiness, priority level, and interoperation dependencies. If all of the queued operations have the same queuePriority and the ready property returns YES, the queue invokes them in the order you added them. Otherwise, the operation queue always invokes the operation with the highest priority relative to the other ready operations.

오퍼레이션 큐는 준비정도, 우선순위레벨, 오퍼레이션간 의존도에 따라 작업을 구성하고 호출합니다. 만약 정렬된 오퍼레이션들이 모두 같은 우선순위, 준비정도를 가진다면 큐는 오퍼레이션들을 추가된 순서로 호출 할 것입니다. 그렇지 않으면, 작업 대기열은 항상 다른 준비된 작업에 비해 우선 순위가 가장 높은 작업을 호출합니다.

However, don't rely on queue semantics to ensure a specific execution order of operations because changes in the readiness of an operation can change the resulting execution order. Interoperation dependencies provide an absolute execution order for operations, even if those operations are located in different operation queues. An operation object isn't ready to run until all of its dependent operations have finished running

그러나, 오퍼레이션의 특정 실행 순서를 보장하기 위해 큐 의미론에 의지하지 마세요. 왜냐하면 오퍼레이션의 준비정도에서 변화들이 결과 실행 순서를 바꿀 수 있기 때문입니다. 상호협력 의존성은 심지어 다른 오퍼레이션 큐에 위치하는 작업이더라도, 오퍼레이션에 절대적 실행 순서를 제공합니다. 오퍼레이션 객체는 모든 종속작업이 완료될 때 까지 실행준비가 되지 않습니다.

For details on how to set priority levels and dependencies, see Managing Dependencies in NSOperation.

Respond to Operation Cancelation

Finishing its task doesn’t necessarily mean that the operation performed that task to completion; an operation can also be canceled. Canceling an operation object leaves the object in the queue but notifies the object that it should stop its task as quickly as possible. For currently executing operations, this means that the operation object’s work code must check the cancellation state, stop what it is doing, and mark itself as finished. For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished.

작업을 끝냈다고 해서 작업이 반드시 그 작업을 완료했다는 것을 의미하지는 않습니다.오퍼레이션은 취소될 수도 있습니다. 작업 개체를 취소하면 개체가 대기열에 남게 되지만, 가능한 빨리 작업을 중지해야 한다고 객체에게 알립니다. 현재 실행중인 작업의 경우, 이것은 작업 객체의 코드가 취소상태를 확인해야만 하고, 하던 것을 멈추고, 끝낸다는 표시를 해야한다는 것을 의미합니다. 대기열이지만 아직 실행되지 않은 작업의 경우, 대기열은 여전히 작업 객체의 시작 메서드를 호출하여 취소 이벤트를 처리하고 완료로 표시할 수 있습니다.

Note :
Canceling an opertion causes the operation to ignore any dependencies it may have. This behavior makes it possible for the queue to invoke the operation's start method as soom as possible. The start method, in turn, moves the operation to the finished state so that it can be removed from the queue

작업을 취소하면, 작업이 가질만한 모든 종속성을 무시하게 됩니다. 이 동작은 큐가 작업의 start 메서드를 가능한 빠르게 호출하는 것은 가능케 합니다. start 메서드는 차례로 작업을 "finish" 상태로 이동하여 대기열에서 제거할 수 있습니다.

For more information about operation cancellation, see Responding to the Cancel Command in NSOperation.

Observe Operations Using Key-Value Observing

The NSOperationQueue class is key-value coding (KVC) and key-value observing (KVO) compliant. You can observe these properties to control other parts of your application. To observe the properties, use the following key paths:

NSOperationQueue 클래스는 키-값 코딩(KVC)과 키-값 관찰(KVO)을 준수합니다. 앱의 다른 파트를 제어하기 위해 이 프로퍼티들을 관찰할 수 있습니다. 이를 위해선 다음의 key paths를 사용합니다

  • operations - Read only
  • operationCount - Read-only
  • maxConcurrentOperationCount - Readable and writable
  • suspended - Readable and writable
  • name - Readable and writable

Although you can attach observers to these properties, don’t use Cocoa bindings to bind these properties to elements of your application’s user interface. Code associated with your user interface typically must run only in your app’s main thread. However, KVO notifications associated with an operation queue may occur in any thread.
For more information about KVO and how to attach observers to an object, see the Key-Value Observing Programming Guide.

비록 이 프로퍼티들에 관찰자를 붙일 수 있지만, 앱의 사용자 인터페이스 요소에 이 프로퍼티들을 할당하기 위해 Cocoa binding을 사용하지 마세요. 당신의 사용자 인터페이스에 연관된 코드는 보통 메인스레드 에서만 동작합니다. 그러나 , 오퍼레이션 큐와 관련된 KVO 알람은 어느 스레드에서나 발생할 수 있습니다.

Plan for Thread Safety

You can safely use a single NSOperationQueue object from multiple threads without creating additional locks to synchronize access to that object.
Operation queues use the Dispatch framework to initiate the execution of their operations. As a result, queues always invoke operations on a separate thread, regardless of whether the operation is synchronous or asynchronous.

당신은 객체에 동기적 접근을 막는 추가적인 잠금장치 없이 안전하게 여러 스레드에서 단일 NSOperationQueue 객체를 사용할 수 있습니다.
오퍼레이션 큐는 그들의 작업을 실행을 초기화하기 위해 Dispatch framework를 사용합니다.
그 결과, 큐는 작업이 동기이든 비동기이든 상관없이 항상 분리된 스레드 위에서 작업을 호출합니다

profile
IOS 앱개발 공부

0개의 댓글