Grand Central Dispatch(GCD)是異步履行義務的技巧之一
dispatch queue分紅以下三種:
1)運轉在主線程的Main queue,經由過程dispatch_get_main_queue獲得。
/*! * @function dispatch_get_main_queue * * @abstract * Returns the default queue that is bound to the main thread. * * @discussion * In order to invoke blocks submitted to the main queue, the application must * call dispatch_main(), NSApplicationMain(), or use a CFRunLoop on the main * thread. * * @result * Returns the main queue. This queue is created automatically on behalf of * the main thread before main() is called. */ __OSX_AVAILABLE_STARTING(__MAC_10_6,__IPHONE_4_0) DISPATCH_EXPORT struct dispatch_queue_s _dispatch_main_q; #define dispatch_get_main_queue() \ DISPATCH_GLOBAL_OBJECT(dispatch_queue_t, _dispatch_main_q)
可以看出,dispatch_get_main_queue也是一種dispatch_queue_t。
2)並行隊列global dispatch queue,經由過程dispatch_get_global_queue獲得,由體系創立三個分歧優先級的dispatch queue。並行隊列的履行次序與其參加隊列的次序雷同。
3)串行隊列serial queues普通用於按次序同步拜訪,可創立隨意率性數目的串行隊列,各個串行隊列之間是並發的。
當想要義務依照某一個特定的次序履行時,串行隊列是很有效的。串行隊列在統一個時光只履行一個義務。我們可使用串行隊列取代鎖去掩護同享的數據。和鎖分歧,一個串行隊列可以包管義務在一個可預知的次序下履行。
serial queues經由過程dispatch_queue_create創立,可使用函數dispatch_retain和dispatch_release去增長或許削減援用計數。
GCD的用法:
// 後台履行:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// something
});
// 主線程履行:
dispatch_async(dispatch_get_main_queue(), ^{
// something
});
// 一次性履行:
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// code to be executed once
});
// 延遲2秒履行:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
// code to be executed on the main queue after delay
});
// 自界說dispatch_queue_t
dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com", NULL);
dispatch_async(urls_queue, ^{
// your code
});
dispatch_release(urls_queue);
// 歸並匯總成果
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 並行履行的線程一
});
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 並行履行的線程二
});
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
// 匯總成果
});
一個運用GCD的例子:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL * url = [NSURL URLWithString:@"http://www.百度.com"];
NSError * error;
NSString * data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (data != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"call back, the data is: %@", data);
});
} else {
NSLog(@"error when download:%@", error);
}
});
GCD的另外一個用途是可讓法式在後台較久長的運轉。
在沒有應用GCD時,當app被按home鍵加入後,app唯一最多5秒鐘的時刻做一些保留或清算資本的任務。然則在應用GCD後,app最多有10分鐘的時光在後台久長運轉。這個時光可以用來做清算當地緩存,發送統計數據等任務。
讓法式在後台久長運轉的示例代碼以下:
// AppDelegate.h文件
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;
// AppDelegate.m文件
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self beingBackgroundUpdateTask];
// 在這裡加上你須要久長運轉的代碼
[self endBackgroundUpdateTask];
}
- (void)beingBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void)endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
以上內容是小編給年夜家引見的IOS中GCD的應用,願望對年夜家有所贊助!
【詳解iOS多線程GCD的應用】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!