The use of nsoperation and Nsoperationqueue for iOS multithreaded programming

Source: Internet
Author: User

Previous article

The use of Nsthread for iOS multithreaded programming

It introduces the use of three kinds of multithreaded programming and Nsthread, this article introduces the use of nsoperation.

There are two ways to use Nsoperation, one is to use two sub-classes that are well-defined:

Nsinvocationoperation and Nsblockoperation.

The other is to inherit Nsoperation

If you are familiar with java,nsoperation, it is similar to the Java.lang.Runnable interface. Like Java's runnable, Nsoperation is also designed to be extended by simply inheriting one of the methods of overriding Nsoperation main. Equivalent to the Runnalbe run method in Java. Then put the object of the Nsoperation subclass into the Nsoperationqueue queue, and the queue will start and process it.

nsinvocationoperation Example:

As in the previous blog post, we implemented an example of downloading a picture. Create a new single View app and drag and drop a ImageView control to the Xib interface.

The implementation code is as follows:

[CPP]View Plaincopy
  1. #import "ViewController.h"
  2. #define KURL @ "Http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"
  3. @interface Viewcontroller ()
  4. @end
  5. @implementation Viewcontroller
  6. -(void) Viewdidload
  7. {
  8. [Super Viewdidload];
  9. Nsinvocationoperation *operation = [[Nsinvocationoperation alloc]initwithtarget:self
  10.                                                                                selector: @selector (downloadimage:)   
  11. Object:kurl];
  12. Nsoperationqueue *queue = [[Nsoperationqueue alloc]init];
  13. [Queue addoperation:operation];
  14. additional setup after loading the view, typically from a nib.
  15. }
  16. -(void) Downloadimage: (NSString *) url{
  17. NSLog (@"url:%@", url);
  18. Nsurl *nsurl = [Nsurl Urlwithstring:url];
  19. NSData *data = [[NSData Alloc]initwithcontentsofurl:nsurl];
  20. UIImage * image = [[UIImage alloc]initwithdata:data];
  21. [Self Performselectoronmainthread: @selector (updateUI:) withobject:image Waituntildone:yes];
  22. }
  23. -(void) UpdateUI: (uiimage*) image{
  24. Self.imageView.image = image;
  25. }

    1. You can see in the Viewdidload method that we built a background thread with nsinvocationoperation and put it in Nsoperationqueue. The background thread executes the Downloadimage method.
    2. The Downloadimage method handles the logic of downloading pictures. After the download is complete, use Performselectoronmainthread to execute the main thread UpdateUI method.
    3. UpdateUI and displays the downloaded picture in the picture control.

Run can see the download picture displayed on the interface.

The second way of inheriting nsoperation

Implement the Main method in the. m file, and the main method writes the code to execute.

How do I control the number of threads in a thread pool?

Many nsoperation can be added to the queue, and nsoperationqueue can be thought of as a thread pool that adds operations (nsoperation) to the queue. Threads in a thread pool can be considered consumers, taken from a queue, and executed.

Set by the following code:
[Queue Setmaxconcurrentoperationcount:5];
The number of threads in the thread pool, which is the number of concurrent operations. By default, -1,-1 represents no limit, which runs all the operations in the queue at the same time.

The use of nsoperation and Nsoperationqueue for iOS multithreaded programming

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.