NSOperation and NSOperationQueue 筆記

WWDC Advanced NSOperations?

https://developer.apple.com/videos/wwdc/2015/?id=226

WWDC app use NSOperation

Download Settings

Download Schedule

Download News

Download Video list

Mark Favorites

Leave Feedback

Log In

Developer Status

Download Pass

Download Photos

Watch Videos

Push Notifications

Access CloudKit

Location Permission

Display Alerts

Upload Analytics

Agenda

Core Concepts

1NSOperationQueue:ahigh-level wrapper around a dispatch queue

1.1 NSOperationQueue makes it easy tocanceloperations that have not yet begun executing

1.2 Serial Queues: set ?maxConcurrentOperationCount=1 ,load a bundle of operations into this queue,the queue will pull off these operations one by one and execute them in order,the next operation will not begin executing until the previous has finished

1.3 Concurrent Queues:setmaxConcurrentOperationCount=default , run as many as system allows,run multiple operationssimultaneously

2 NSOperation:a high-levelwrapperaround a dispatch block

2.1 it’s class , you can subclass it,provide your own custom logic on how it executes

2.2 lifecycle


2.2.1? cancel

can not change from the “finished” state to the “cancelled” state , ? If you want cancel an operation ,there are some cases where won’t actually cancel

call cancel method

operationA.cancel()

2.2.2 ready

Serial Queues:which one is ready will execute first , maybe not the the first one pull into the queue

2.3 ?dependency:express a strict ordering

an operation will become ready if all of its dependencies have finished executing

not limited by queues:If you have two operation queues in your application,operations in the first queue can be dependent on the operations in the second queue

let operationA = ...

let operationB = ...

operationB.addDependency(operationA)


Beyond the Basics

1 UI Operations ,not just the background

Authentication Videos

Alerts

“Modal” UI

2 Block Operations

It’s blocks all the way down

NSOperation to execute a block

Dependencies for blocks

Leaving feedback

3 GeneratingDependencies

When two operations love each other...

“Never execute this until after executing that”

4Mutual Exclusivity

There can be only one

cross-queue dependencies are really powerful

Alerts

Videos

Loading database

Sample Code

Operation

NSOperation subclass

Adds “conditions”

Adds “observers”

Example operations

Groups

URLSessionTask

Location

Delay

OperationCondition

Generates dependencies

Defines mutual exclusivity

Checks for satisfied conditions

Example conditions

MutuallyExclusive

Reachability

Permissions

OperationObserver

Notified about significant events

Operation start

Operation end

Operation generation

Example observers

Timeouts

Background tasks

Network activity indicator

Summary

Operations abstract logic

Dependencies clarify intent

Describe complex behaviors

Enables powerful patterns

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容