ios34---GDC,dispatch_once

來源:互聯網
上載者:User

標籤:bsp   port   uitouch   token   atomic   interface   amp   ted   main   

////  ViewController.m//  09-掌握-GCD常用函數////  Created by xiaomage on 16/2/18.//  Copyright ? 2016年 小碼哥. All rights reserved.//#import "ViewController.h"#import "XMGPerson.h"@interface ViewController ()@end@implementation ViewController-(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);}//順延強制-(void)delay{    NSLog(@"start-----");        //1. 順延強制的第一種方法    //[self performSelector:@selector(task) withObject:nil afterDelay:2.0];        //2.順延強制的第二種方法    //[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);    /*     第一個參數:DISPATCH_TIME_NOW 從現在開始計算時間     第二個參數:延遲的時間 2.0 GCD時間單位:納秒     第三個參數:隊列     */    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), queue, ^{        NSLog(@"GCD----%@",[NSThread currentThread]);    });}//一次性代碼//不能放在懶載入中的,應用情境:單例模式-(void)once{    static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{  //整個專案生命週期只執行一次,多次調用都不會執行多次        NSLog(@"---once----");    });}-(void)task{    NSLog(@"task----%@",[NSThread currentThread]);}@end
////  XMGPerson.h//  09-掌握-GCD常用函數////  Created by xiaomage on 16/2/18.//  Copyright ? 2016年 小碼哥. All rights reserved.//#import <Foundation/Foundation.h>@interface XMGPerson : NSObject@property (nonatomic, strong) NSArray *books;@end
////  XMGPerson.m//  09-掌握-GCD常用函數////  Created by xiaomage on 16/2/18.//  Copyright ? 2016年 小碼哥. All rights reserved.//#import "XMGPerson.h"@implementation XMGPerson-(NSArray *)books{//    if (_books == nil) {//        _books = @[@"1234",@"56789"];//    }        static dispatch_once_t onceToken;    dispatch_once(&onceToken, ^{  //只執行一次        _books = @[@"1234",@"56789"];    });    return _books;}@end

 

ios34---GDC,dispatch_once

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.