1:釋放單例的方法
static dispatch_once_t onceToken;
onceToke = 0;
2:NSNotificationCenter 在哪個線程post則在哪個線程轉(zhuǎn)發(fā)丈秩,不是add時候的線程
主線程執(zhí)行
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti) name:@"abc" object:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//打印[NSThread currentThread]
[[NSNotificationCenter defaultCenter] postNotificationName:@"abc" object:@"a"];
});
- (void)noti{
//打印[NSThread currentThread]
NSLog(@"noti");
}
結(jié)果:
<NSThread: 0x60000026bd00>{number = 3, name = (null)}
<NSThread: 0x60000026bd00>{number = 3, name = (null)}
如果noti方法是要更新UI赁严,請確保post在主線程上或者在noti方法內(nèi)處理一下岂嗓。
3:注冊通訊錄變化通知
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged2, nil);
只能注冊一次玖瘸,否則當(dāng)通訊錄改變時职烧,注冊多少次就會調(diào)用多少次addressBookChanged2
4:NSTimer
invalidate方法調(diào)用必須在timer添加到的runloop所在的線程
http://www.cnblogs.com/mddblog/p/6517377.html
5:等待兩個請求完成之后再更新UI
方法一:http://www.reibang.com/p/943dcb9ad632
方法二:http://www.reibang.com/p/228403206664
dispatch_group_enter(dispatchGroup);
dispatch_group_leave(dispatchGroup);
dispatch_queue_t queue = dispatch_queue_create("getHomePageQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[self.dataRequestUC requestHomePageDataWithParama:@{} finishBlock:^(id requestResult,NSInteger statusCode) {
if (requestResult) {
__strong typeof(weakSelf) strongSelf = weakSelf;
NSMutableArray *home = [NSMutableArray arrayWithArray:[strongSelf corprationCMSData:requestResult]];
strongSelf.homePageData = [home mutableCopy];
dispatch_group_leave(group);
}
}];
dispatch_group_enter(group);
[self.dataRequestUC requestGJMessageWithParama:@{@"pageSize":@"3",@"pageNo":@"1"} finishBlock:^(id requestResult, NSInteger statusCode) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if ([requestResult isKindOfClass:[NSDictionary class]]) {
NSDictionary *body = requestResult[@"body"];
NSArray *data = body[@"data"];
[strongSelf corperationMessage:data];
dispatch_group_leave(group);
}
}];
long timeout = dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, (int64_t)10*NSEC_PER_SEC));
if (timeout) {
dataBlock(nil);
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
dataBlock([self combineData]);
});
dispatch_group_wait不能在主線程使用貌踏,否則會阻塞十气。
方法三:RAC
方法四:dispatch_barrier_async
方法五:NSOperationQueue addDependency
6:自定義NSOperation
重寫start方法支持異步操作,但是需要管理狀態(tài)瑟枫,并且以KVO的方式進(jìn)行實現(xiàn)
重寫main方法不用管理operation的狀態(tài)斗搞,為了能夠使用操作隊列的取消功能,需要不斷的檢查isCancel
- (void)main
{
while (notDone && !self.isCancelled) {
// 進(jìn)行處理
}
}
7:在 Objective-C 中將屬性以 atomic 的形式來聲明慷妙,就能支持互斥鎖了僻焚。默認(rèn)就為atomic,但是開銷較大景殷。
加鎖解鎖有性能消耗溅呢,當(dāng)獲取鎖的時候澡屡,如果屬性已經(jīng)某一個線程操作猿挚,則當(dāng)前線程需要等待,等屬性被操作完畢驶鹉,解鎖后才能繼續(xù)進(jìn)行绩蜻。
8:如果你在 table view 或者是 collection view 的 cell 上做了自定義繪制的話,最好將它們放入 operation 的子類中去室埋。你可以將它們添加到后臺操作隊列办绝,也可以在用戶將 cell 滾動出邊界時的 didEndDisplayingCell 委托方法中進(jìn)行取消
9:在后臺或者子線程訪問UIKit/APPKit等屬性,如設(shè)置image則設(shè)置生效的時間會延后姚淆。如果有兩個及以上的線程同時進(jìn)行訪問或設(shè)置孕蝉,會造成時機性的崩潰問題。
10:iOS中用來表示內(nèi)存的堆棧與數(shù)據(jù)結(jié)構(gòu)中的堆棧代表的意義不一樣
棧:先進(jìn)后出
全局和靜態(tài)變量在堆中
指針放在棧中腌逢,APP通過在棧中找到數(shù)據(jù)的指針降淮,指向堆中的數(shù)據(jù)。
11:創(chuàng)建靜態(tài)庫搏讶,使用cocoapod管理第三方依賴佳鳖。pod install以后會自動在link Binary With Libraries中將工程名的靜態(tài)庫放進(jìn)去,導(dǎo)致編譯報錯
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: can't locate file for: -lPods-projectName
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: file: -lPods-projectName is not an object file (not allowed in a library)
手動刪除后媒惕,編譯成功系吩。