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


NSOperation and NSOperationQueue 笔记_第1张图片

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

NSOperation and NSOperationQueue 笔记_第2张图片

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)


NSOperation and NSOperationQueue 笔记_第3张图片

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

你可能感兴趣的:(NSOperation and NSOperationQueue 笔记)