Pick up an article
2, Nsoperation
Nsoperation is an abstract class, and there is no way to use it directly, and if we want to use it, we need to use its subclasses. iOS has provided us with two sub-categories, nsinvocationoperation and nsblockoperation that have been implemented well.
For these two subclasses although iOS itself provides the Start method, we generally do not use it directly and need to take advantage of another class Nsoperationqueue.
Nsoperationqueue as the name implies is an operation queue, he will provide us with an orderly execution queue, look at Apple's official description of this class
The NsoperationqueueclassRegulates theExecution ofASet ofNsoperation objects. After being added toA queue, an operation remainsinch thatQueueuntil it isExplicitly canceledorFinishes executing itsTask. Operations within theQueue ( but notYet executing) is themselves organized according toPriority levels andInter-operation Object Dependencies andis executed accordingly. AnApplicationMay create multiple operation queues andSubmit Operations toAny ofthem. Inter-operation dependencies provide an absolute execution order forOperations, evenifThose operations is locatedinchDifferent operation queues. An Operation object is notConsidered ready toExecuteuntilAll of itsDependent operations has finished executing. For operations thatis ready toExecute theOperation Queue always executes theOne with theHighest priority relative to theOther ready operations. For details onHow to SetPriority levels anddependencies, see Nsoperation Class reference.you cannot directly remove a operation fromA queue After ithas been added. An operation remainsinch itsQueueuntil itReports that it isFinished with itsTask. Finishing itsTaskdoes notnecessarily mean that theOperation performed thatTask toCompletion. An operation can also is canceled. Canceling an Operation object leaves theObjectinch theQueue butNotifies theObject that itShould abort itsTask asQuickly asPossible. For currently executing operations, this means that theOperation Object ' s work code must check theCancellation state, stop whatit isDoing andMark itself asFinished. For operations thatIs queued but notYet executing, theQueue must still call theOperation Object ' s Start method so that itCan processes theCancellation Event andMark itself asFinished.
Presumably it means that the Nsoperationqueue class regulates the execution of a set of Nsoperation objects. The Nsoperation objects in the queue are executed in an absolute order, meaning that when a task is added to the task queue, it automatically runs according to priority and dependency.
Or the above example?
-(void) viewdidload {[Super viewdidload]; do any Additional setup after loading the view , Typically from a nib.//nsthread *mthread = [[Nsthread alloc] initwithtarget:self sel Ector: @selector (downimage:) Object:image_path]; [Mthread start ]; Nsinvocationoperation *moperation = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (downimage :) object:image_path];//[moperation start ]; Nsoperationqueue *mqueue = [[Nsoperationqueue alloc] init]; [Mqueue addoperation:moperation];}
The
looks simple simply by creating a task and then joining the queue. This queue is a task pool that follows the producer-consumer relationship and automatically runs the task when there is a task. We can use Setmaxconcurrentoperationcount: This method to set the total number of threads in this queue, the default value is-1, meaning there is no limit.
After inheriting nsoperation, for non-concurrent work, the main method of the Nsoperation subclass needs to be implemented:
-(void) Main
{
@try
{
Working with work tasks
}
@catch (...)
{
Handles exceptions, but cannot re-throw exceptions
}
}
Because Nsoperation's task can be cancel, the main method of handling the task is to constantly poll iscancelled. In addition, this main method is inherently empty, so there is no need to invoke the phrase "super main".
There is also a Start method, which is the entry for the work, and is usually used to set the running environment that the thread needs. As with main, do not invoke [super start]. This method also distinguishes the state of the operation, and if it is canceled or is finished, the task will not run, and if it is running or not ready, an exception will be thrown.
If you want to support concurrent tasks, at a minimum, you need to rewrite the start, isconcurrent, Isexecuting, isfinished four methods. Here is the Isconcurrent method, which is whether the flag operation is executed in parallel, if it is concurrent, returns Yes; If you do not override this method, the default is no, but after OS X10.6, this value is ignored.
In addition, some of the properties of Nsoperation are supported by KVC, and we can use the Kvo method to observe the properties and control the program to run elsewhere in the application, so KVO notifications need to be made at the appropriate time.
The KVO attributes supported by Nsoperation are: iscancelled, isconcurrent, isexecuting, isfinished, IsReady, dependencies, queuepriority, Completionblock. If you add attributes, it is recommended to also support KVC and KVO.
Threading Operations in iOS (2)