這里的單例無(wú)效,指的是雖然用了singleton模式,本該在APP生命周期內(nèi)真椿,只有一個(gè)單例對(duì)象鹃答,一個(gè)內(nèi)存地址,卻出現(xiàn)多個(gè)對(duì)象的問(wèn)題突硝。如下是singleton模式代碼:
@implementation AObject
+ (instancetype)shareInstance {
static dispatch_once_t onceToken;
static AObject *instance;
dispatch_once(&onceToken, ^{
instance = [AObject new];
NSLog(@"AObject: %p", instance);
instance.name = @"A";
});
return instance;
}
@end
基本上通過(guò)[AObject shareInstance]测摔,在工程任何地方取得的對(duì)象,都是同一個(gè)內(nèi)存地址的單例對(duì)象解恰。但發(fā)現(xiàn)某種存在動(dòng)態(tài)庫(kù)情況下锋八,出現(xiàn)[AObject shareInstance]取到多個(gè)不同對(duì)象的情況。
單例無(wú)效demo測(cè)試
有個(gè)簡(jiǎn)單的測(cè)試demo
目錄結(jié)構(gòu):
.
├── LA0
├── LB0
├── LC0
├── LC1
├── MainProj
└── Test.xcworkspace
工程結(jié)構(gòu):
MainProj
------------------
LC0 | LC1
------------------
LB0
------------------
LA0
------------------
Test workspace中护盈,MainProj是主工程挟纱,LC0、LC1腐宋、LB0紊服、LA0都是依賴的framework。如圖示胸竞,MainProj依賴LC0围苫、LC1;LC0撤师、LC1依賴LB0;LB0依賴LA0拧揽。但在build phases中剃盾,上層的link binary with libraries不僅將直屬下層加入,同時(shí)也將下層的下層加入淤袜。例如LC0的配置:
單例類AObject在LA0中痒谴,上層庫(kù)、主工程都會(huì)調(diào)用單例對(duì)象铡羡。
1. 所有framework都是static library
設(shè)置LC0积蔚、LC1、LB0烦周、LA0的Build Settings->Mach-O Type->static library尽爆。
運(yùn)行工程(注:運(yùn)行前需要清空products文件夾下之前編譯好的framework、.app读慎,保證環(huán)境clear)漱贱,console:
2018-03-01 16:06:31.850360+0800 MainProj[24755:2092911] AObject: 0x6000000123d0
單例對(duì)象[AObject shareInstance]只有一個(gè)內(nèi)存地址。單例模式?jīng)]有問(wèn)題夭委。
2. 設(shè)置LA0為dynamic library幅狮,其他framework都是static library
2018-03-01 16:08:29.750429+0800 MainProj[24800:2095054] AObject: 0x600000012a80
單例模式?jīng)]有問(wèn)題
3. 設(shè)置LB0為dynamic library,其他framework都是static library
objc[50214]: Class AObject is implemented in both /Users/hotacool/Library/Developer/Xcode/DerivedData/Test-fsrnousjgrbqxngqgmulsskbrcln/Build/Products/Debug-iphonesimulator/LB0.framework/LB0 (0x10daf6418) and /Users/hotacool/Library/Developer/CoreSimulator/Devices/E925C3DF-D46D-4DCF-9936-9239176C4189/data/Containers/Bundle/Application/BA8871EE-BFBA-4F94-9A05-EE56FF8B5758/MainProj.app/MainProj (0x10d8112b0). One of the two will be used. Which one is undefined.
(void *) $0 = 0x00006040001ad580
2018-03-06 09:39:58.536791+0800 MainProj[50214:3929509] AObject: 0x6040002033f0
2018-03-06 09:39:58.537293+0800 MainProj[50214:3929509] AObject: 0x6000000197e0
單例對(duì)象[AObject shareInstance]出現(xiàn)兩個(gè)不同的內(nèi)存地址。根據(jù)console提示信息崇摄,AObject類在LB0.framework和.app中都有實(shí)現(xiàn)擎值。
4. 設(shè)置LC0為dynamic library,其他framework都是static library
objc[50340]: Class AObject is implemented in both /Users/hotacool/Library/Developer/Xcode/DerivedData/Test-fsrnousjgrbqxngqgmulsskbrcln/Build/Products/Debug-iphonesimulator/LC0.framework/LC0 (0x106ed5590) and /Users/hotacool/Library/Developer/CoreSimulator/Devices/E925C3DF-D46D-4DCF-9936-9239176C4189/data/Containers/Bundle/Application/8C4343C0-DF3F-442B-9CF2-3898EC678F39/MainProj.app/MainProj (0x106bef2b0). One of the two will be used. Which one is undefined.
objc[50340]: Class BObject is implemented in both /Users/hotacool/Library/Developer/Xcode/DerivedData/Test-fsrnousjgrbqxngqgmulsskbrcln/Build/Products/Debug-iphonesimulator/LC0.framework/LC0 (0x106ed55e0) and /Users/hotacool/Library/Developer/CoreSimulator/Devices/E925C3DF-D46D-4DCF-9936-9239176C4189/data/Containers/Bundle/Application/8C4343C0-DF3F-442B-9CF2-3898EC678F39/MainProj.app/MainProj (0x106bef300). One of the two will be used. Which one is undefined.
(void *) $0 = 0x00006000001bdf80
2018-03-06 09:47:11.090783+0800 MainProj[50340:3937289] AObject: 0x604000011dd0
2018-03-06 09:47:11.091422+0800 MainProj[50340:3937289] AObject: 0x604000011d70
同樣出現(xiàn)單例對(duì)象多個(gè)地址問(wèn)題逐抑。并且根據(jù)提示信息鸠儿,AObject 、BObject都出現(xiàn)重復(fù)實(shí)現(xiàn)泵肄。
5. 所有framework都是dynamic library
2018-03-06 09:55:09.844002+0800 MainProj[50456:3944829] AObject: 0x604000015eb0
單例模式?jīng)]有問(wèn)題
推測(cè)結(jié)論
通過(guò)上述測(cè)試捆交,只有當(dāng)dynamic library依賴static library,上層同時(shí)添加dynamic library和static library到link binary library中時(shí)腐巢,會(huì)發(fā)生單例對(duì)象多個(gè)地址品追,如上面demo中static library中對(duì)象在.app和dynamic library多個(gè)實(shí)現(xiàn)的問(wèn)題。
原因推測(cè)是:編譯dynamic library時(shí)冯丙,會(huì)將依賴static library編譯為二進(jìn)制集成到dynamic library中肉瓦,同時(shí).app也會(huì)將static library編譯到二進(jìn)制中,導(dǎo)致有多個(gè)二進(jìn)制實(shí)現(xiàn)胃惜,單例對(duì)象的調(diào)用地址會(huì)在編譯時(shí)在二進(jìn)制中寫好泞莉,導(dǎo)致調(diào)用時(shí)有多個(gè)對(duì)象地址。
避免上述問(wèn)題船殉,一方面是梳理清楚依賴關(guān)系鲫趁,如demo中,有很多多余的依賴利虫,例如LC0的link binary libraries中只需加入LB0即可挨厚,無(wú)需LA0,通過(guò)刪除多余的依賴糠惫,也可以保證不出現(xiàn)單例無(wú)效的問(wèn)題疫剃。另一方面,在項(xiàng)目架構(gòu)設(shè)計(jì)中硼讽,盡量避免dynamic和static相互依賴的情況巢价,在物理層面上完全杜絕。