iOS multithreading--nsoperation common methods

Source: Internet
Author: User

1 Maximum number of concurrent

Max Concurrency: The maximum number of tasks that can be performed at the same time.
The most significant concurrency number of the correlation? method

- (NSInteger)maxConcurrentOperationCount;- (void)setMaxConcurrentOperationCount:(NSInteger)cnt; 

Note: If the maximum number of concurrent numbers is not set, then the number of concurrent is determined by the system memory and CPU.
Tip: The maximum number of concurrent not too much (within 5), generally more than the appropriate, because although the task is processed on the child thread, but the child threads will occupy the system memory, while the CPU processing these excessive sub-threads may affect the UI, let the UI change card, affect the user experience.
code example:

- (void) blockoperation3{//Create global Queue (for asynchronous concurrent execution)Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Specifies the maximum number of concurrent    //When the maximum concurrent number is 1, the queue becomes a serial queueQueue. Maxconcurrentoperationcount=3;//Create a taskNsblockoperation *operation1 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 1---%@", [NsthreadCurrentThread]);    }]; Nsblockoperation *operation2 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download Picture 2---%@", [NsthreadCurrentThread]);    }]; Nsblockoperation *operation3 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 3---%@", [NsthreadCurrentThread]);    }]; Nsblockoperation *operation4 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 4---%@", [NsthreadCurrentThread]);    }]; Nsblockoperation *operation5 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 5---%@", [NsthreadCurrentThread]); }];//Add a task to the queue[Queue Addoperation:operation1];    [Queue Addoperation:operation2];    [Queue Addoperation:operation3];    [Queue Addoperation:operation4]; [Queue addoperation:operation5];}


2 adding dependencies
- (void) blockoperation4{//Create global Queue (for asynchronous concurrent execution)Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Create a taskNsblockoperation *operation1 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 1---%@", [NsthreadCurrentThread]);    }]; Nsblockoperation *operation2 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download Picture 2---%@", [NsthreadCurrentThread]);    }]; Nsblockoperation *operation3 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 3---%@", [NsthreadCurrentThread]);    }]; [Operation1 Adddependency:operation3];//Task 1 dependent Task 3 (perform task 3 to perform task 1)[Operation3 Adddependency:operation2];//Task 3 Dependent Task 2 (perform task 2 to perform task 3)    //Add a task to the queue[Queue Addoperation:operation1];    [Queue Addoperation:operation2]; [Queue Addoperation:operation3];}


Attention:
Can I set dependencies between nsoperation to guarantee the order of execution? If you want to make operation 3 complete, you can perform operation 1 as follows

[operation1 addDependency:operation3]; // 操作1依赖于操作3

You can create a dependency between nsoperation of different queue

Note: Cannot cycle dependent (cannot rely on b,b and depends on a)

3 Process Communication
- (void)sendMessage{    //创建全局队列(实现异步并发执行)    NSOperationQueue *queue = [[NSOperationQueue alloc] init];    //直接向队列中添加任务    [queue addOperationWithBlock:^{        //耗时操作        NSLog(@"下载图片1 --- %@", [NSThread currentThread]);        //回到主线程        [[NSOperationQueue mainQueue] addOperationWithBlock:^{            //刷新UI操作        }];    }];}

Summary: Methods to return to the main thread:

//回到主线程    [self performSelectorOnMainThread:<#(nonnull SEL)#> withObject:<#(nullable id)#> waitUntilDone:<#(BOOL)#>]    dispatch_sync(dispatch_get_main_queue(), ^{        //刷新UI操作    });    [[NSOperationQueue mainQueue] addOperationWithBlock:^{        //刷新UI操作    }];
Cancel Pause Restart Queue

(1) Cancel all operations of the queue

 - (void)cancelAllOperations;

You can also call the Nsoperation-(void) Cancel method to cancel a single operation

(2) Pausing and resuming queues

// YES代表暂停队列,NO代表恢复队列- (void)setSuspended:(BOOL)b; - (BOOL//当前状态

(3) The application of pause and resume: In the TableView interface, the remote Web interface can be downloaded remotely, which will affect the UI and make the user experience worse. In this case, you can set the queue to pause (not cancel the queue) when the user operates the UI (such as scrolling the screen), to stop scrolling, and to resume the queues.

//取消队列中所有任务(收到内存警告的时候)[queue cancelAllOperations];//暂停队列中的所有任务(开始拖动操作等)[queue setSuspended:YES];//恢复队列中的所有任务(结束拖动操作等)[queue setSuspended:NO];
Listener Thread Execution
//可以监听一个操作的执行完毕- (void (^)(void))completionBlock;- (void)setCompletionBlock:(void (^)(void))block;

To implement the functions of the GCD queue group:

- (void) groupoperation{//Create global Queue (for asynchronous concurrent execution)Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Create a taskNsblockoperation *operation1 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 1---%@", [NsthreadCurrentThread]);    }]; [Operation1 addexecutionblock:^{NSLog(@"Download Picture 2---%@", [NsthreadCurrentThread]); }];//When the thread finishes executing, call the[Operation1 setcompletionblock:^{//Merge pictures 1 and 2        NSLog(@"Merging pictures 1 and 2---%@", [NsthreadCurrentThread]); }];//Add a task to the queue[Queue Addoperation:operation1]; }

iOS multithreading--nsoperation common methods

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.