生活随笔
收集整理的這篇文章主要介紹了
IOS之GCD基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS之GCD基礎
1.柵欄函數的使用
#import "ViewController.h"
@implementation ViewController
- (void)touchesBegan
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{dispatch_queue_t queue
= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT
, 0);dispatch_async(queue
, ^{NSLog(@"%@",[NSThread currentThread
]);});dispatch_async(queue
, ^{NSLog(@"%@",[NSThread currentThread
]);});dispatch_queue_t queue1
= dispatch_queue_create("download", DISPATCH_QUEUE_CONCURRENT
);dispatch_barrier_async(queue1
, ^{NSLog(@"++++++++");});dispatch_async(queue
, ^{NSLog(@"%@",[NSThread currentThread
]);});
}
@end
2.GCD 的快速迭代
文件快速復制案例
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)touchesBegan
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{
[self moveFileWithGCD
];
}
-(void)forDemo
{for(NSInteger i
=0;i
<10;i
++){NSLog(@"%zd----%@",i
,[NSThread currentThread
]);}
}
-(void)apply
{dispatch_apply(10, dispatch_get_global_queue(0, 0) , ^(size_t index
) {NSLog(@"%zd------%@",index
,[NSThread currentThread
]);});
}
-(void)moveFile
{NSString
*from
= @"/Users/lujun/Desktop/from";NSString
*to
= @"/Users/lujun/Desktop/to";NSArray
*subPaths
= [[NSFileManager defaultManager
] subpathsAtPath
:from
];NSLog(@"%@",subPaths
);NSInteger count
= subPaths
.count
;for(NSInteger i
=0;i
< count
;i
++){NSString
*fullPath
= [from stringByAppendingPathComponent
:subPaths
[i
]];NSString
*toFullPath
= [to stringByAppendingPathComponent
:subPaths
[i
]];NSLog(@"%@",fullPath
);BOOL b
= [[NSFileManager defaultManager
] moveItemAtPath
:fullPath toPath
:toFullPath error
:nil
];NSLog(@"%@,%@,%@",fullPath
,toFullPath
,[NSThread currentThread
]);}
}-(void)moveFileWithGCD
{NSString
*from
= @"/Users/lujun/Desktop/from";NSString
*to
= @"/Users/lujun/Desktop/to";NSArray
*subPaths
= [[NSFileManager defaultManager
] subpathsAtPath
:from
];NSLog(@"%@",subPaths
);NSInteger count
= subPaths
.count
;dispatch_apply(count
, dispatch_get_global_queue(0, 0), ^(size_t i
) {NSString
*fullPath
= [from stringByAppendingPathComponent
:subPaths
[i
]];NSString
*toFullPath
= [to stringByAppendingPathComponent
:subPaths
[i
]];NSLog(@"%@",fullPath
);BOOL b
= [[NSFileManager defaultManager
] moveItemAtPath
:fullPath toPath
:toFullPath error
:nil
];NSLog(@"%@,%@,%@",fullPath
,toFullPath
,[NSThread currentThread
]);});}
@end
03- GCD 隊列組的基本使用
#import "ViewController.h"
@implementation ViewController
- (void)touchesBegan
:(NSSet
<UITouch
*> *)touches withEvent
:(UIEvent
*)event
{dispatch_queue_t queue
=dispatch_get_global_queue(0, 0);dispatch_group_t group
= dispatch_group_create();dispatch_async(queue
, ^{NSLog(@"1--- %@",[NSThread currentThread
]);});dispatch_async(queue
, ^{NSLog(@"2--- %@",[NSThread currentThread
]);});dispatch_async(queue
, ^{NSLog(@"3--- %@",[NSThread currentThread
]);});
}
@end
總結
以上是生活随笔為你收集整理的IOS之GCD基础的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。