iOS-多線程筆記

來源:互聯網
上載者:User

標籤:

零、線程的注意點(掌握)
1.不要同時開太多的線程(1~3條線程即可,不要超過5條)
2.線程概念
1> 主線程 : UI線程,顯示、重新整理UI介面,處理UI控制項的事件
2> 子線程 : 後台線程,非同步線程
3.不要把耗時的操作放在主線程,要放在子線程中執行

一、NSThread(掌握)
1.建立和啟動線程的3種方式
1> 先建立,後啟動
// 建立
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(download:) object:nil];
// 啟動
[thread start];

2> 建立完自動啟動
[NSThread detachNewThreadSelector:@selector(download:) toTarget:self withObject:nil];

3> 隱式建立(自動啟動)
[self performSelectorInBackground:@selector(download:) withObject:nil];

2.常見方法
1> 獲得當前線程
+ (NSThread *)currentThread;

2> 獲得主線程
+ (NSThread *)mainThread;

3> 睡眠(暫停)線程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;

4> 設定線程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;

二、線程同步(掌握)
1.實質:為了防止多個線程搶奪同一個資源造成的資料安全問題

2.實現:給代碼加一個互斥鎖(同步鎖)
@synchronized(self) {
// 被鎖住的代碼
}

三、GCD
1.隊列和任務
1> 任務 :需要執行什麼操作
* 用block來封裝任務

2> 隊列 :存放任務
* 全域的並發隊列 : 可以讓任務並發執行
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

* 自己建立的串列隊列 : 讓任務一個接著一個執行
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);

* 主隊列 : 讓任務在主線程執行
dispatch_queue_t queue = dispatch_get_main_queue();

2.執行任務的函數
1> 同步執行 : 不具備開啟新線程的能力
dispatch_sync...

2> 非同步執行 : 具備開啟新線程的能力
dispatch_async...

3.常見的組合(掌握)
1> dispatch_async + 全域並發隊列
2> dispatch_async + 自己建立的串列隊列

4.線程間的通訊(掌握)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 執行耗時的非同步作業...


dispatch_async(dispatch_get_main_queue(), ^{
// 回到主線程,執行UI重新整理操作
});
});

5.GCD的所有API都在libdispatch.dylib,Xcode會自動匯入這個庫
* 主標頭檔 : #import <dispatch/dispatch.h>

6.順延強制(掌握)
1> perform....
// 3秒後自動回到當前線程調用self的download:方法,並且傳遞參數:@"http://555.jpg"
[self performSelector:@selector(download:) withObject:@"http://555.jpg" afterDelay:3];

2> dispatch_after...
// 任務放到哪個隊列中執行
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
double delay = 3; // 延遲多少秒
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), queue, ^{
// 3秒後需要執行的任務
});

7.一次性代碼(掌握)
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// 這裡面的代碼,在程式運行過程中,永遠只會執行1次
});

四、單例模式(懶漢式)
1.ARC
@interface HMDataTool : NSObject
+ (instancetype)sharedDataTool;
@end

@implementation HMDataTool
// 用來儲存唯一的單例對象
static id _instace;

+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}

+ (instancetype)sharedDataTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc] init];
});
return _instace;
}

- (id)copyWithZone:(NSZone *)zone
{
return _instace;
}
@end

2.非ARC
@interface HMDataTool : NSObject
+ (instancetype)sharedDataTool;
@end

@implementation HMDataTool
// 用來儲存唯一的單例對象
static id _instace;

+ (id)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [super allocWithZone:zone];
});
return _instace;
}

+ (instancetype)sharedDataTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instace = [[self alloc] init];
});
return _instace;
}

- (id)copyWithZone:(NSZone *)zone
{
return _instace;
}

- (oneway void)release {

}

- (id)retain {
return self;
}

- (NSUInteger)retainCount {
return 1;
}

- (id)autorelease {
return self;
}
@end

五、NSOperation和NSOperationQueue
1.隊列的類型
1> 主隊列
* [NSOperationQueue mainQueue]
* 添加到"主隊列"中的操作,都會放到主線程中執行

2> 非主隊列
* [[NSOperationQueue alloc] init]
* 添加到"非主隊列"中的操作,都會放到子線程中執行

2.隊列新增工作
* - (void)addOperation:(NSOperation *)op;
* - (void)addOperationWithBlock:(void (^)(void))block;

3.常見用法
1> 設定最大並發數
- (NSInteger)maxConcurrentOperationCount;
- (void)setMaxConcurrentOperationCount:(NSInteger)cnt;

2> 隊列的其他動作
* 取消所有的操作
- (void)cancelAllOperations;

* 暫停所有的操作
[queue setSuspended:YES];

* 恢複所有的操作
[queue setSuspended:NO];

4.操作之間的依賴(面試題)
* NSOperation之間可以設定依賴來保證執行順序
* [operationB addDependency:operationA];
// 操作B依賴於操作A,等操作A執行完畢後,才會執行操作B
* 注意:不能相互依賴,比如A依賴B,B依賴A
* 可以在不同queue的NSOperation之間建立依賴關係

5.線程之間的通訊
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
// 1.執行一些比較耗時的操作

// 2.回到主線程
[[NSOperationQueue mainQueue] addOperationWithBlock:^{

}];
}];

六、從其他線程回到主線程的方式
1.perform...
[self performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>];

2.GCD
dispatch_async(dispatch_get_main_queue(), ^{

});

3.NSOperationQueue
[[NSOperationQueue mainQueue] addOperationWithBlock:^{

}];

七、判斷編譯器的環境:ARC還是MRC?
#if __has_feature(objc_arc)
// 當前的編譯器環境是ARC

#else
// 當前的編譯器環境是MRC

#endif

八、類的初始化方法
1.+(void)load
* 當某個類第一次裝載到OC運行時系統(記憶體)時,就會調用
* 程式一啟動就會調用
* 程式運行過程中,只會調用1次

2.+(void)initialize
* 當某個類第一次被使用時(比如調用了類的某個方法),就會調用
* 並非程式一啟動就會調用

3.在程式運行過程中:1個類中的某個操作,只想執行1次,那麼這個操作放到+(void)load方法中最合適

九、第三方架構的使用建議
1.用第三方架構的目的
1> 開發效率:快速開發,人家封裝好的一行代碼頂自己寫的N行
2> 為了使用這個功能最牛逼的實現

2.第三方架構過多,很多壞處(忽略不計)
1> 管理、升級、更新
2> 第三方架構有BUG,等待作者解決
3> 第三方架構的作者不幸去世、停止更新(潛在的BUG無人解決)
4> 感覺:自己好水

3.比如
流媒體:播放線上視頻、音頻(邊下載邊播放)
非常瞭解音頻、視頻檔案的格式
每一種視頻都有自己的解碼方式(C\C++)

4.總結
1> 站在巨人的肩膀上編程
2> 沒有關係,使勁用那麼比較穩定的第三方架構

十、cell的圖片下載
1.面試題
1> 如何防止一個url對應的圖片重複下載
* “cell下載圖片思路 – 有沙箱緩衝”

2> SDWebImage的預設緩衝時間長度是多少?
* 1個星期

3> SDWebImage底層是怎麼實現的?
* 上課PPT的“cell下載圖片思路 – 有沙箱緩衝”

2.SDWebImage
1> 常用方法
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder completed:(SDWebImageCompletionBlock)completedBlock;
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;

2> 記憶體處理:當app接收到記憶體警告時
/**
* 當app接收到記憶體警告
*/
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application
{
SDWebImageManager *mgr = [SDWebImageManager sharedManager];

// 1.取消正在下載的操作
[mgr cancelAll];

// 2.清除記憶體緩衝
[mgr.imageCache clearMemory];
}

3> SDWebImageOptions
* SDWebImageRetryFailed : 下載失敗後,會自動重新下載
* SDWebImageLowPriority : 當進行中UI互動時,自動暫停內部的一些下載操作
* SDWebImageRetryFailed | SDWebImageLowPriority : 擁有上面2個功能

iOS-多線程筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.