IOS34---gdc,dispatch_once

Source: Internet
Author: User
Tags gcd

////VIEWCONTROLLER.M//09-Mastering-GCD Common functions////Created by Xiaomage on 16/2/18.//2016 little brother. All rights reserved.//#import "ViewController.h"#import "XMGPerson.h"@interfaceViewcontroller ()@end@implementationViewcontroller-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event{//[self once];Xmgperson*P1 =[[Xmgperson alloc]init]; Xmgperson*P2 =[[Xmgperson alloc]init]; NSLog (@"%@---%@", p1.books,p2.books);}//deferred execution-(void) delay{NSLog (@"Start-----"); //1. The first method of deferred execution//[Self Performselector: @selector (Task) Withobject:nil afterdelay:2.0]; //2. Second method of deferred execution//[Nstimer scheduledtimerwithtimeinterval:2.0 target:self selector: @selector (Task) Userinfo:nil Repeats:yes]; //3.GCD//dispatch_queue_t queue = Dispatch_get_main_queue ();dispatch_queue_t queue = Dispatch_get_global_queue (0,0); /*First parameter: Dispatch_time_now calculate time from now the second parameter: Delay time 2.0 GCD time unit: nanosecond third parameter: Queue*/Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), queue, ^{NSLog (@"GCD----%@", [Nsthread CurrentThread]); });}//Disposable Code//can not be placed in lazy loading, application scenario: Singleton mode-(void) once{Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{//the entire project life cycle is performed only once, and multiple calls are not executed more than onceNSLog (@"---once----"); });}-(void) task{NSLog (@"Task----%@", [Nsthread CurrentThread]);}@end
// //   XMGPerson.h//  09-Mastering-GCD Common functions ////  Created by Xiaomage on 16/2/18. //   2016 little brother. All rights reserved. // #import <Foundation/Foundation.h>@interface*books; @end
////xmgperson.m//09-Mastering-GCD Common functions////Created by Xiaomage on 16/2/18.//2016 little brother. All rights reserved.//#import "XMGPerson.h"@implementationXmgperson-(Nsarray *) books{//if (_books = = nil) {//_books = @[@ "1234", @ "56789"];//    }        Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{//Execute only once_books = @[@"1234",@"56789"];    }); return_books;}@end

IOS34---gdc,dispatch_once

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.