Multi-threaded usage in IOS development

Source: Internet
Author: User

One, five ways to create multi-threading
1. How to turn on threading
    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<; i++ {            printf (" sub-thread \ n");}    }];
5. How to turn on threading five
    //The first step is to turn on the thread poolNsoperationqueue * queue=[[Nsoperationqueue alloc] init]; //set number of concurrent[Queue Setmaxconcurrentoperationcount:2]; //Second Create multithreaded add to thread poolNsinvocationoperation * 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:

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

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and the blog Park is shared, welcome reprint, but without the consent of the author must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

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.