1 前言
當執行那些與 UI 無關的任務,或者與 UI 交互的任務時,和執行其他任務一樣,會需要大量時間,以上情況會經常出現。我們可以使用 dispatch_sync函數在一個分派隊列上執行同步任務。你必須做的事情就是提供一個此隊列的句柄了,這個隊列必須運行任務,並且一個代碼塊會在這個隊列上執行。 今天我們就來學習一下GCD執行非UI的操作。
2 代碼實例
TestDemo.h
[plain]
#import <Foundation/Foundation.h>
@interface TestDemo : NSObject
-(void)testMethod;
-(void)testMethod2;
@end
#import <Foundation/Foundation.h>
@interface TestDemo : NSObject
-(void)testMethod;
-(void)testMethod2;
@end
TestDemo.m
[plain]
#import "TestDemo.h"
@implementation TestDemo
/**************Objc Method Start*********/
//Block Object
void (^printFrom1To1000)(void) = ^{
NSUInteger counter = 0;
for (counter = 1;counter <= 1000;counter++){
NSLog(@"Counter = %lu - Thread = %@",
(unsigned long)counter,
[NSThread currentThread]);
}
};
//測試方法
-(void)testMethod{
/*
Dispatch_get_global_queue 函數的第一個參數說明了並發隊列的優先級,這個屬性 GCD 必須替程序員檢
索。優先級越高,將提供更多的 CPU Timeslice 來獲取該隊列執行的代碼。
Dispatch_get_global_queue 函數的第一個參數:
DISPATCH_QUEUE_PRIORITY_LOW
您的任務比正常任務用到更少的 Timeslice。
DISPATCH_QUEUE_PRIORITY_DEFAULT
執行代碼的默認系統優先級將應用於您的任務。
DISPATCH_QUEUE_PRIORITY_HIGH
和正常任務相比,更多的 Timeslices 會應用到你的任務中。
Dispatch_get_global_queue 函數的第二個參數已經保存了,只要一直給它輸入數值 0 就可以了。
*/
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(concurrentQueue, printFrom1To1000);
dispatch_sync(concurrentQueue, printFrom1To1000);
}
/**************Objc Method End*********/
/**************C Method Start*********/
//Block Object
void print2From1To1000(void *paramContext){
NSUInteger counter = 0; for (counter = 1;counter <= 1000;counter++){
NSLog(@"Counter = %lu - Thread = %@",
(unsigned long)counter, [NSThread currentThread]);
}
}
//測試方法
-(void)testMethod2{
/*
*/
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync_f(concurrentQueue, NULL,print2From1To1000);
dispatch_sync_f(concurrentQueue, NULL,print2From1To1000);
}
/**************C Method End*********/
@end
#import "TestDemo.h"
@implementation TestDemo
/**************Objc Method Start*********/
//Block Object
void (^printFrom1To1000)(void) = ^{
NSUInteger counter = 0;
for (counter = 1;counter <= 1000;counter++){
NSLog(@"Counter = %lu - Thread = %@",
(unsigned long)counter,
[NSThread currentThread]);
}
};
//測試方法
-(void)testMethod{
/*
Dispatch_get_global_queue 函數的第一個參數說明了並發隊列的優先級,這個屬性 GCD 必須替程序員檢
索。優先級越高,將提供更多的 CPU Timeslice 來獲取該隊列執行的代碼。
Dispatch_get_global_queue 函數的第一個參數:
DISPATCH_QUEUE_PRIORITY_LOW
您的任務比正常任務用到更少的 Timeslice。
DISPATCH_QUEUE_PRIORITY_DEFAULT
執行代碼的默認系統優先級將應用於您的任務。
DISPATCH_QUEUE_PRIORITY_HIGH
和正常任務相比,更多的 Timeslices 會應用到你的任務中。
Dispatch_get_global_queue 函數的第二個參數已經保存了,只要一直給它輸入數值 0 就可以了。
*/
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync(concurrentQueue, printFrom1To1000);
dispatch_sync(concurrentQueue, printFrom1To1000);
}
/**************Objc Method End*********/
/**************C Method Start*********/
//Block Object
void print2From1To1000(void *paramContext){
NSUInteger counter = 0; for (counter = 1;counter <= 1000;counter++){
NSLog(@"Counter = %lu - Thread = %@",
(unsigned long)counter, [NSThread currentThread]);
}
}
//測試方法
-(void)testMethod2{
/*
*/
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_sync_f(concurrentQueue, NULL,print2From1To1000);
dispatch_sync_f(concurrentQueue, NULL,print2From1To1000);
}
/**************C Method End*********/
@end
main.m
[plain]
import <Foundation/Foundation.h>
#import "TestDemo.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
TestDemo *test = [[TestDemo alloc] init];
// [test testMethod];
[test testMethod2];
[test release];
}
return 0;
}
#import <Foundation/Foundation.h>
#import "TestDemo.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
TestDemo *test = [[TestDemo alloc] init];
// [test testMethod];
[test testMethod2];
[test release];
}
return 0;
}
運行結果
2013-05-10 13:50:37.361 GCDExecuteNonUITest[556:303] Counter = 1 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}
2013-05-10 13:50:37.363 GCDExecuteNonUITest[556:303] Counter = 2 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}
...
2013-05-10 13:50:38.255 GCDExecuteNonUITest[556:303] Counter = 1 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}
2013-05-10 13:50:38.256 GCDExecuteNonUITest[556:303] Counter = 2 - Thread = <NSThread: 0x10010b2c0>{name = (null), num = 1}