NSInvocationOperation multithreading tutorial for iPhone Development

Source: Internet
Author: User

IPhone DevelopmentAbout NSInvocationOperationMultithreadingThe tutorial is the content to be introduced in this article, mainly to introduceThreadFor more information, see.MultithreadingProgramming is to prevent the masterThreadBlocking, increase operation efficiency, and so on. While the originalMultithreadingThere are many problems with the method, includingThreadLocked.

In Cocoa, Apple provides the NSOperation class and an excellent multi-threaded programming method. This section describes the subset of NSOperation and NSInvocationOperation of the simple method:

 
 
  1. @ Implementation MyCustomClass
  2. -(Void) launchTaskWithData :( id) data {
  3. // Create an NSInvocationOperation object and initialize it to the Method
  4. // Here, the value after the selector parameter is the Method function you want to run in another thread. Method)
  5. // Here, the object value is the data to be passed to the previous method.
  6. NSInvocationOperation * theOp = [[NSInvocationOperation alloc]
  7. InitWithTarget: selfselector: @ selector (myTaskMethod :) object: data];
  8. // Add the Operation "Operation" we created to the shared queue of the local program and the method will be executed immediately)
  9. // In more cases, we create an "operation" queue by ourselves.
  10. [[MyAppDelegate implements doperationqueue] addOperation: theOp];
  11. }
  12. // This is the "method" that actually runs in another thread"
  13. -(Void) myTaskMethod :( id) data {
  14. // Perform the task.
  15. }
  16. End

An NSOperationQueue operation queue is equivalent to a thread manager rather than a thread. Because you can set the number of threads that can run in parallel in this thread manager. The following describes how to create and initialize an operation queue:

 
 
  1. @ Interface MyViewController: UIViewController {
  2. NSOperationQueue * operationQueue;
  3. // Declare the queue in the header file
  4. }
  5. @ End
  6. @ Implementation MyViewController
  7. -(Id) init {self = [super init];
  8. If (self) {operationQueue = [[NSOperationQueue alloc] init];
  9. // Initialize the operation queue
  10. [OperationQueue setMaxConcurrentOperationCount: 1];
  11. // This limits the queue to run only one thread at a time
  12. // This queue is ready for use.
  13. }
  14. Return self;
  15. }
  16. -(Void) dealloc {
  17. [OperationQueue release];
  18. // As Alan often said, we are a good citizen of the program and need to release the memory!
  19. [Super dealloc];
  20. }
  21. @ End

After a brief introduction, we can find that this method is very simple. Many times we useMultithreadingOnly to prevent the masterThreadAnd NSInvocationOperation is the simplestMultithreadingProgramming is often used in iPhone programming.

Summary:IPhone DevelopmentAbout NSInvocationOperationMultithreadingThe content of this tutorial has been introduced. I hope this article will help you!

Related Article

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.