Multi-thread NSInvocationOperation learning for IOS development

Source: Internet
Author: User

IOS developmentInMulti-thread NSInvocationOperationLearning is the content to be introduced in this article, mainly to learnIOS developmentInMultithreadingFor more information, see the details of this article.

MultithreadingProgramming is the best way to prevent the main thread from being congested and increase the running efficiency. The original multi-threaded method has many problems, including thread locking. 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. {
  4. // Create an NSInvocationOperation object and initialize it to the Method
  5. // Here, the value after the selector parameter is the Method function you want to run in another thread. Method)
  6. // Here, the object value is the data to be passed to the previous method.
  7. NSInvocationOperation * theOp = [[NSInvocationOperation alloc] initWithTarget: self
  8. Selector: @ selector (myTaskMethod :) object: data];
  9. // Add the Operation "Operation" we created to the shared queue of the local program and the method will be executed immediately)
  10. // In more cases, we create an "operation" queue by ourselves.
  11. [[MyAppDelegate implements doperationqueue] addOperation: theOp];
  12. }
  13. // This is the "method" that actually runs in another thread"
  14. -(Void) myTaskMethod :( id) data
  15. {
  16. // Perform the task.
  17. }

@ 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
  8. {
  9. Self = [super init];
  10. If (self ){
  11. OperationQueue = [[NSOperationQueue alloc] init]; // initialize the operation queue
  12. [OperationQueue setMaxConcurrentOperationCount: 1];
  13. // This limits the queue to run only one thread at a time
  14. // This queue is ready for use.
  15. }
  16. Return self;
  17. }
  18. -(Void) dealloc
  19. {
  20. [OperationQueue release];
  21. // As Alan often said, we are a good citizen of the program and need to release the memory!
  22. [Super dealloc];
  23. }
  24. @ End

After a brief introduction, we can find that this method is very simple. In many cases, multithreading is only used to prevent main thread congestion, and NSInvocationOperation is the simplest multi-threaded programming, which is often used in iPhone programming.

Summary:IOS developmentOfMulti-thread NSInvocationOperationI hope this article will be helpful to 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.