Multi-threaded usage in IOS development

Source: Internet
Author: User

One, five ways to create multi-threading
1. How to turn on threading
    Nsthread * Thread=[[nsthread alloc] initwithtarget:self selector: @selector (_update) Object:nil];
2. How to turn on threading method two
    [Nsthread detachnewthreadselector: @selector (_update) totarget:self Withobject:nil];
3. How to turn on threading three
    [Self Performselectorinbackground: @selector (_update) Withobject:nil];
4. How to turn on threading four
    Nsoperationqueue *queue=[[nsoperationqueue alloc] init];    [Queue addoperationwithblock:^{for        (int i=0;i<50;i++) {            printf ("sub-thread \ n");        }    ];
5. How to turn on threading five
<pre name= "code" class= "OBJC" >    //First step to open thread pool        nsoperationqueue * Queue=[[nsoperationqueue alloc] init];    Set number of concurrent    [queue setmaxconcurrentoperationcount:2];        Second create multithreaded add to thread pool    nsinvocationoperation * thread1=[[nsinvocationoperation alloc] initwithtarget:self selector:@ Selector (_update1) Object:nil];    Nsinvocationoperation *thread2=[[nsinvocationoperation alloc] initwithtarget:self selector: @selector (_UPDATE2) Object:nil];        [Thread1 Setqueuepriority:nsoperationqueuepriorityverylow];    [Thread2 Setqueuepriority:nsoperationqueuepriorityveryhigh];        [Queue addoperation:thread1];    [Queue addoperation:thread2];
Second, multi-threaded application examples, loading pictures.
1. Core Ideas

Consider that if loading a network picture is delayed, in one mainline preempted will affect the rendering of the control, at which point you can take multi-threading and refresh the UI after the asynchronous loading is complete.

2. Realization of Ideas

Multi-threaded downloads are achieved by adding classes to the Uiimageview.

  Main code:

#import "Uiimageview+thread.h" @implementation uiimageview (load)-(void) Setimagewithurl: (NSString *) url{    [self Performselectorinbackground: @selector (_loadimage:) Withobject:url];} -(void) _loadimage: (NSString *) u{    @autoreleasepool {                nsurl *url=[nsurl urlwithstring:u];        NSData *data=[nsdata Datawithcontentsofurl:url];        UIImage *image=[uiimage Imagewithdata:data];                [Self Performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no];            }}

Multi-threaded usage in IOS development

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.