GCD work Unit

Source: Internet
Author: User
Tags uikit

#import <UIKit/UIKit.h>

@interface Viewcontroller:uiviewcontroller


@property (weak,nonatomic) iboutlet UIButton *startbtn;
@property (weak,nonatomic) iboutlet Uitextview *resultstextview;
@property (weak,nonatomic) iboutlet Uiactivityindicatorview *spinner;

@end

Viewcontroller.m

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

-(NSString *) fetchsomethingfromserver{

[Nsthread sleepfortimeinterval:1];

return @ "Hi there";
}

-(NSString *) ProcessData: (NSString *) data{
[Nsthread Sleepfortimeinterval:2];
return [data uppercasestring];
}
-(NSString *) Calculatefirstresult: (NSString *) data{
[Nsthread Sleepfortimeinterval:3];
return [NSString stringwithformat:@ "Number of Chars:%lu", (unsigned long) [data length]];
}

-(NSString *) Calculatesecondresult: (NSString *) data{
[Nsthread Sleepfortimeinterval:4];
return [Data stringbyreplacingoccurrencesofstring:@ "E" withstring:@ "E"];
}

-(Ibaction) DoWork: (ID) sender{


NSDate *starttime = [NSDate Date];
Add indicator Code
self.startBtn.enabled = NO;

[Self.spinner startanimating];

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
Dispatch_async (Queue, ^{

NSString *fetcheddata = [self fetchsomethingfromserver];
NSString *processeddata = [self processdata:fetcheddata];
NSString *firstresult = [self calculatefirstresult:processeddata];
NSString *senconresult = [self calculatesecondresult:processeddata];
NSString *resultsummary = [NSString stringwithformat:@ "first:[%@]\nsecond:[%@]", Firstresult,senconresult];


Dispatch_async (Dispatch_get_main_queue (), ^{

Self.resultsTextView.text = resultsummary;
});

NSDate *endtime = [NSDate Date];
NSLog (@ "completed in%f seconds", [EndTime timeintervalsincedate:starttime]);

});


1 NSString *fetcheddata = [self fetchsomethingfromserver];
NSString *processeddata = [self processdata:fetcheddata];
NSString *firstresult = [self calculatefirstresult:processeddata];
NSString *senconresult = [self calculatesecondresult:processeddata];
NSString *resultsummary = [NSString stringwithformat:@ "first:[%@]\nsecond:[%@]", Firstresult,senconresult];

Self.resultsTextView.text = resultsummary;
NSDate *endtime = [NSDate Date];
NSLog (@ "completed in%f seconds", [EndTime timeintervalsincedate:starttime]);
}


@end

GCD work Unit

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.