iOS開發中GCD在多線程方面的理解,ios開發gcd多線程
GCD為Grand Central Dispatch的縮寫。 Grand Central Dispatch (GCD)是Apple開發的一個多核編程的較新的解決方案。在Mac OS X 10.6雪豹中首次推出,並在最近引入到了iOS4.0。 GCD是一個替代諸如NSThread等技術的很高效和強大的技術。GCD完全可以處理諸如資料鎖定和資源泄漏等複雜的非同步編程問題。
GCD可以完成很多事情,但是這裡僅關注在iOS應用中實現多線程所需的一些基礎知識。 在開始之前,需要理解是要提供給GCD隊列的是代碼塊,用於在系統或者使用者建立的的隊列上調度運行。 聲明一個隊列
如下會返回一個使用者建立的隊列:
dispatch_queue_t myQueue = dispatch_queue_create("com.iphonedevblog.post", NULL);其中,第一個參數是標識隊列的,第二個參數是用來定義隊列的參數(目前不支援,因此傳入NULL)。
執行一個隊列
如下會非同步執行傳入的代碼:
dispatch_async(myQueue, ^{ [self doSomething]; });其中,首先傳入之前建立的隊列,然後提供由隊列啟動並執行代碼塊。
聲明並執行一個隊列
如果不需要保留要啟動並執行隊列的引用,可以通過如下代碼實現之前的功能: dispatch_async(dispatch_queue_create ("com.iphonedevblog.post", NULL), ^{ [self doSomething]; }); 如果需要暫停一個隊列,可以調用如下代碼。暫停一個隊列會阻止和該隊列相關的所有代碼運行。 dispatch_suspend(myQueue);暫停一個隊列
如果暫停一個隊列不要忘記恢複。暫停和恢複的操作和記憶體管理中的retain和release類似。調用dispatch_suspend會增加暫停計數,而dispatch_resume則會減少。隊列只有在暫停計數變成零的情況下才開始運行。dispatch_resume(myQueue);恢複一個隊列 從隊列中在主線程運行代碼 有些操作無法在非同步隊列運行,因此必須在主線程(每個應用都有一個)上運行。UI繪圖以及任何對NSNotificationCenter的調用必須在主線程長進行。在另一個隊列中訪問主線程並運行代碼的樣本如下: dispatch_sync(dispatch_get_main_queue(), ^{ [self dismissLoginWindow]; });注意,dispatch_suspend (以及dispatch_resume)在主線程上不起作用。
使用GCD,可以讓你的程式不會失去響應. 多線程不容易使用,用了GCD,會讓它變得簡單。你無需專門進行線程管理, 很棒!
讓你的程式保持響應的原則:
1. 不要柱塞主線程
2. 把工作一到其他線程中做。
3. 做完後更新主線程的UI.
舉例說明:
沒有GCD的代碼:
- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {
// 在主線程調用。
DTweet *tw = [[DTweet alloc] initWithMsg:msg];
[tweets addTweet:tw display:YES];
tw.img = [imageCache getImgFromURL:url];//bottle neck
[tweets updateTweet:tw display:YES];
[tw release];
}
有GCD的代碼:
- (void)addTweetWithMsg:(NSString*)msg url:(NSURL*)url {
//在主線程調用。
DTweet *tw = [[DTweet alloc] initWithMsg:msg];
[tweets addTweet:tw display:YES];
dispatch_async(image_queue, ^{
tw.img = [imageCache getImgFromURL:url];//放到一個非同步隊列裡。
dispatch_async(main_queue, ^{
[tweets updateTweet:tw display:YES];//放到非同步主線程裡。
});
});
[tw release];
}
1. GCD is part of libSystem.dylib
2. #include <dispatch/dispatch.h>
一》NSThread的方法:代碼如下:
- (void)viewDidLoad
{
[super viewDidLoad];
NSThread *thread1=[[NSThread alloc]initWithTarget:self selector:@selector(print1) object:nil];
[thread1 start];
NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(print2) object:nil];
[thread2 start];
}
-(void)print1{
for (int i=0; i<100; i++) {
NSLog(@"我是print1正在執行%d",i);
}
}
-(void)print2{
for (int i=0; i<100; i++) {
NSLog(@"print2正在執行%d",i);
}
}
二》
NSInvocationOperation
的方法:代碼如下
// NSInvocationOperation *operation1=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print1) object:@"1"];
// NSInvocationOperation *operation2=[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(print2) object:@"2"];//當然這裡可以用一個方法。
// NSOperationQueue *queue=[[NSOperationQueue alloc]init];
// [queue addOperation:operation1];
// [queue addOperation:operation2];
三》
GCD
的方法:代碼如下
dispatch_queue_t t1=dispatch_queue_create("1", NULL);
dispatch_queue_t t2=dispatch_queue_create("2", NULL);
dispatch_async(t1, ^{
[self print1];
});
dispatch_async(t2, ^{
[self print2];
});
http://www.cnblogs.com/pengyingh/articles/2356825.htmlhttp://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html //詳解地址。http://blog.csdn.net/zhuqilin0/article/details/6527113 //訊息推送機制
Push的原理:
Push 的工作機制可以簡單的概括為
圖中,Provider是指某個iPhone軟體的Push伺服器,這篇文章我將使用.net作為Provider。 APNS 是Apple Push Notification Service(Apple Push伺服器)的縮寫,是蘋果的伺服器。
可以分為三個階段。
第一階段:.net應用程式把要發送的訊息、目的iPhone的標識打包,發給APNS。 第二階段:APNS在自身的登入Push服務的iPhone列表中,尋找有相應標識的iPhone,並把訊息發到iPhone。 第三階段:iPhone把發來的訊息傳遞給相應的應用程式, 並且按照設定彈出Push通知。
http://blog.csdn.net/zhuqilin0/article/details/6527113 //訊息推送機制看記憶體泄露時候:在搜尋中搜尋run 找到Run Static Snalyzer .
多線程在ios開發中的做用,常用的多線程類與方法有什
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
dispatch_async(kSystemAppUpdateQueue, ^{
NSError *error;
NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]
options:0
error:&error];
if (error!=nil) {
NSLog(@"%@",error.description);
[self performSelectorOnMainThread:selectorError withObject:nil waitUntilDone:YES];
}
else{
if (data!=nil)
{
[self performSelectorOnMainThread:selectorDone withObject:data waitUntilDone:YES];
}
else
{
[self performSelectorOnMainThread:selectorError withObject:nil waitUntilDone:YES];
}
}
});
return;
一個iOS開發,多線程,runloop學習的Demo
同求!!!!!!!!!!!