Simple usage of iosnsoperation

Source: Internet
Author: User
Tags gcd

Today, physically and mentally exhausted Ah, these days run legs are soft, have a friend in Shanghai? I am looking for a job, need a job, see the peer to help me notice. Thank you so much. My QQ is 820203420. At the same time I also hope to continue to bring you better articles, there is time I will open source a project, I hope you notice, or you can contact me, we open source together. Good night.

#import "ViewController.h"


@interface viewcontroller ()


@end


@implementation Viewcontroller


-(void) viewdidload {

[superviewdidload];


#if 0

//Nsoperation/nsoperationqueue is an object-oriented multithreaded class GCD based on the GCD package is a pure C-language GC D relatively efficient

//nsoperation/nsoperationqueue Development Steps

//1. Create nsoperation object that encapsulates the task in the object.

//2. Create Nsoperationqueue operation queue, which will nsoperation The object is thrown into the operations queue.

//3. after the operation is added to the operation queue, the system automatically takes the actions in the queue and puts them on the thread.

//nsoperation is an abstract class ( a class that cannot instantiate an object ). He has two sub-categories .

//Nsinvocationoperation

//Nsblockoperation

// Create an Action object, you must use these subclasses to instantiate

///nsoperationqueue operation queue only two kinds,

//1. Custom Action queue, by alloc Init created by. The task for this queue executes concurrently in the child thread.

//2. Home Row similar to the GCD master queue, The tasks that are added to the master queue are executed in the main thread.

//[Nsoperationqueue mainqueue]; How to get the home row

// Create an Action object that encapsulates the task.

nsinvocationoperation *invoperation = [[nsinvocationoperation alloc]initwithtarget: Self Selector:@selector(Test) object:nil];

// perform the task in action. executed in the main thread by default

[Invoperation start];

// Create operation, task saved in block

Nsblockoperation *blockoperation = [Nsblockoperation blockoperationwithblock:^{

NSLog (@ "blockoperation thread =%@", [Nsthread CurrentThread]);

}];

for (int i =0; i< ; i++) {

[Blockoperation addexecutionblock:^{

NSLog (@ "blockoperation%d thread =%@", I,[nsthread CurrentThread]);

}];

}

// perform the task in action. executed in the main thread by default, if the number of tasks in Blockoperation is greater than 1 , the other tasks are executed in the child thread

[Blockoperation start];

#else

//---------- actual use ------------------

//1. Create an Action object

nsinvocationoperation *invocationop = [[nsinvocationoperation Alloc]initwithtarget:selfselector: @selector (test) Object : Nil ];

nsblockoperation *blockop = [nsblockoperation Blockoperationwithblock: ^{

for (int i =0; i<5; i++) {

NSLog(@ "Blockop thread =%@", [nsthreadcurrentthread]);

}

}];

//2. adding an Action object to the operations queue , tasks that are added to the queue are automatically executed in the child threads.

// Create action queue

nsoperationqueue *queue = [[nsoperationqueuealloc ]init];

[Queue addoperation: Invocationop];

[Queue addoperation: Blockop];

for (int i =0; i<5; i++) {

nsblockoperation *bop = [nsblockoperationBlockoperati Onwithblock: ^{

NSLog(@ "Blockop i =%d thread =%@", i,[nsthread CurrentThread ]);

}];

// Add to the main queue the task is executed sequentially in the main thread.

//[[Nsoperationqueue Mainqueue] addoperation:bop];

}

#endif

}

-(void) test

{

for (int i =0; i<5; i++ ) {

NSLog(@ "invocationoperation thread =%@", [nsthread CurrentThread ]);

}

}

@end

Simple usage of iosnsoperation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.