{"rsdb":{"rid":"345945","subhead":"","postdate":"0","aid":"246439","fid":"89","uid":"1","topic":"1","content":"
\n

\u4e0d\u4f1a\u9020\u6210\u5faa\u73af\u5f15\u7528\u7684block<\/span><\/h1> \n

\u5927\u90e8\u5206GCD\u65b9\u6cd5<\/span><\/h2> \n
\n
1<\/span> dispatch_async(dispatch_get_main_queue(), ^{\r\n<\/span>2<\/span>     [self doSomething];\r\n<\/span>3<\/span> });<\/pre> \n <\/div> \n 

\u56e0\u4e3aself\u5e76\u6ca1\u6709\u5bf9GCD\u7684block\u8fdb\u884c\u6301\u6709\uff0c\u6ca1\u6709\u5f62\u6210\u5faa\u73af\u5f15\u7528\u3002\u76ee\u524d\u6211\u8fd8\u6ca1\u78b0\u5230\u4f7f\u7528GCD\u5bfc\u81f4\u5faa\u73af\u5f15\u7528\u7684\u573a\u666f\uff0c\u5982\u679c\u67d0\u79cd\u573a\u666fself\u5bf9GCD\u7684block\u8fdb\u884c\u4e86\u6301\u6709\uff0c\u5219\u624d\u6709\u53ef\u80fd\u9020\u6210\u5faa\u73af\u5f15\u7528\u3002<\/p> \n

 <\/p> \n

block\u5e76\u4e0d\u662f\u5c5e\u6027\u503c\uff0c\u800c\u662f\u4e34\u65f6\u53d8\u91cf<\/span><\/h2> \n
\n
 1<\/span> - (void<\/span>)doSomething {\r\n<\/span> 2<\/span>     [self testWithBlock:^{\r\n<\/span> 3<\/span>         [self test];\r\n<\/span> 4<\/span>     }];\r\n<\/span> 5<\/span> }\r\n<\/span> 6<\/span> \r\n 7<\/span> - (void<\/span>)testWithBlock:(void<\/span>(^)())block {\r\n<\/span> 8<\/span>     block();\r\n<\/span> 9<\/span> }\r\n<\/span>10<\/span> \r\n11<\/span> - (void<\/span>)test {\r\n<\/span>12<\/span>     NSLog(@"<\/span>test<\/span>"<\/span>);\r\n<\/span>13<\/span> }<\/pre> \n <\/div> \n 

\u8fd9\u91cc\u56e0\u4e3ablock\u53ea\u662f\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0cself\u5e76\u6ca1\u6709\u5bf9\u5176\u6301\u6709\uff0c\u6240\u4ee5\u6ca1\u6709\u9020\u6210\u5faa\u73af\u5f15\u7528<\/p> \n

 <\/p> \n

block\u4f7f\u7528\u5bf9\u8c61\u88ab\u63d0\u524d\u91ca\u653e<\/span><\/h2> \n

\u6709\u8fd9\u79cd\u60c5\u51b5\uff0c\u5982\u679c\u4e0d\u53ea\u662fClassA\u6301\u6709\u4e86myBlock\uff0cClassB\u4e5f\u6301\u6709\u4e86myBlock\u3002<\/p> \n

\"\"<\/p> \n

\u5f53ClassA\u88absomeObj\u5bf9\u8c61\u91ca\u653e\u540e<\/p> \n

\"\"<\/p> \n

\u6b64\u65f6\uff0cClassA\u5bf9\u8c61\u5df2\u7ecf\u88ab\u91ca\u653e\uff0c\u800cmyBlock\u8fd8\u662f\u88abClassB\u6301\u6709\uff0c\u6ca1\u6709\u91ca\u653e\uff1b\u5982\u679cmyBlock\u8fd9\u4e2a\u65f6\u88ab\u8c03\u5ea6\uff0c\u800c\u6b64\u65f6ClassA\u5df2\u7ecf\u88ab\u91ca\u653e\uff0c\u6b64\u65f6\u8bbf\u95ee\u7684ClassA\u5c06\u662f\u4e00\u4e2anil\u5bf9\u8c61\uff08\u4f7f\u7528__weak<\/code>\u4fee\u9970\uff0c\u5bf9\u8c61\u91ca\u653e\u65f6\u4f1a\u7f6e\u4e3anil\uff09\uff0c\u800c\u5f15\u53d1\u9519\u8bef\u3002<\/p> \n

 <\/p> \n

\u53e6\u4e00\u4e2a\u5e38\u89c1\u9519\u8bef\u4f7f\u7528\u662f\uff0c\u5f00\u53d1\u8005\u62c5\u5fc3\u5faa\u73af\u5f15\u7528\u9519\u8bef\uff08\u5982\u4e0a\u6240\u8ff0\u4e0d\u4f1a\u51fa\u73b0\u5faa\u73af\u5f15\u7528\u7684\u60c5\u51b5\uff09\uff0c\u4f7f\u7528__weak<\/code>\u3002\u6bd4\u5982<\/p> \n

\n
1<\/span> __weak typeof<\/span>(self) weakSelf = self;\r\n<\/span>2<\/span> dispatch_async(dispatch_get_main_queue(), ^{\r\n<\/span>3<\/span>     [weakSelf doSomething];\r\n<\/span>4<\/span> });<\/pre> \n <\/div> \n 
\n

\u56e0\u4e3a\u5c06block\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9dispatch_async\u65f6\uff0c\u7cfb\u7edf\u4f1a\u5c06block\u62f7\u8d1d\u5230\u5806\u4e0a\uff0c\u800c\u4e14block\u4f1a\u6301\u6709block\u4e2d\u7528\u5230\u7684\u5bf9\u8c61\uff0c\u56e0\u4e3adispatch_async\u5e76\u4e0d\u77e5\u9053block\u4e2d\u5bf9\u8c61\u4f1a\u5728\u4ec0\u4e48\u65f6\u5019\u88ab\u91ca\u653e\uff0c\u4e3a\u4e86\u786e\u4fdd\u7cfb\u7edf\u8c03\u5ea6\u6267\u884cblock\u4e2d\u7684\u4efb\u52a1\u65f6\u5176\u5bf9\u8c61\u6ca1\u6709\u88ab\u610f\u5916\u91ca\u653e\u6389\uff0cdispatch_async\u5fc5\u987b\u81ea\u5df1retain\u4e00\u6b21\u5bf9\u8c61\uff08\u5373self\uff09\uff0c\u4efb\u52a1\u5b8c\u6210\u540e\u518drelease\u5bf9\u8c61\uff08\u5373self\uff09\u3002\u4f46\u8fd9\u91cc\u4f7f\u7528__weak<\/code>\uff0c\u4f7fdispatch_async\u6ca1\u6709\u589e\u52a0self\u7684\u5f15\u7528\u8ba1\u6570\uff0c\u8fd9\u4f7f\u5f97\u5728\u7cfb\u7edf\u5728\u8c03\u5ea6\u6267\u884cblock\u4e4b\u524d\uff0cself\u53ef\u80fd\u5df2\u88ab\u9500\u6bc1\uff0c\u4f46\u7cfb\u7edf\u5e76\u4e0d\u77e5\u9053\u8fd9\u4e2a\u60c5\u51b5\uff0c\u5bfc\u81f4block\u6267\u884c\u65f6\u8bbf\u95ee\u5df2\u7ecf\u88ab\u91ca\u653e\u7684self\uff0c\u800c\u8fbe\u4e0d\u5230\u9884\u671f\u7684\u7ed3\u679c\u3002<\/p> \n

 <\/p> \n

\u6ce8\uff1a\u5982\u679c\u662f\u5728MRC\u6a21\u5f0f\u4e0b\uff0c\u4f7f\u7528__block<\/code>\u4fee\u9970self,\u5219\u6b64\u65f6block\u8bbf\u95ee\u88ab\u91ca\u653e\u7684self\uff0c\u5219\u4f1a\u5bfc\u81f4crash\u3002 <\/h4> \n <\/div> \n

\u8be5\u573a\u666f\u4e0b\u7684\u4ee3\u7801<\/p> \n

\n
 1<\/span> \/\/<\/span> ClassA.m<\/span>\r\n 2<\/span> - (void<\/span>)test {\r\n<\/span> 3<\/span>     __weak MyClass* weakSelf = self;\r\n<\/span> 4<\/span>     double<\/span> delayInSeconds = 10.0f<\/span>;\r\n<\/span> 5<\/span>     dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));\r\n<\/span> 6<\/span>     dispatch_after(popTime, dispatch_get_main_queue(), ^(void<\/span>){\r\n<\/span> 7<\/span>         NSLog(@"<\/span>%@<\/span>"<\/span>, weakSelf);\r\n<\/span> 8<\/span>     });\r\n<\/span> 9<\/span> }\r\n<\/span>10<\/span> \r\n11<\/span> \/\/<\/span> ClassB.m<\/span>\r\n12<\/span> - (void<\/span>)doSomething {\r\n<\/span>13<\/span>     NSLog(@"<\/span>do something<\/span>"<\/span>);\r\n<\/span>14<\/span>     ClassA *objA = [[ClassA alloc] init];\r\n<\/span>15<\/span>     [objA test];\r\n<\/span>16<\/span> }<\/pre> \n <\/div> \n 

\u8fd0\u884c\u7ed3\u679c<\/p> \n

\n
[5988<\/span>:435396<\/span>] do<\/span> something\r\n[<\/span>5988<\/span>:435396<\/span>] self:(null<\/span>)<\/pre> \n <\/div> \n 

\u89e3\u51b3\u65b9\u6cd5\uff1a<\/p> \n

\u5bf9\u4e8e\u8fd9\u79cd\u573a\u666f\uff0c\u5c31\u4e0d\u5e94\u8be5\u4f7f\u7528__weak<\/code>\u6765\u4fee\u9970\u5bf9\u8c61\uff0c\u8ba9dispatch_after\u5bf9self\u8fdb\u884c\u6301\u6709\uff0c\u4fdd\u8bc1block\u6267\u884c\u65f6self\u8fd8\u672a\u88ab\u91ca\u653e\u3002<\/p> \n

 <\/p> \n

\n
\n

block\u6267\u884c\u8fc7\u7a0b\u4e2d\u5bf9\u8c61\u88ab\u91ca\u653e<\/span><\/h2> \n

\u8fd8\u6709\u4e00\u79cd\u573a\u666f\uff0c\u5728block\u6267\u884c\u5f00\u59cb\u65f6self\u5bf9\u8c61\u8fd8\u672a\u88ab\u91ca\u653e\uff0c\u800c\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0cself\u88ab\u91ca\u653e\u4e86\uff0c\u6b64\u65f6\u8bbf\u95eeself\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u9519\u8bef\u3002<\/p> \n

\u5bf9\u4e8e\u8fd9\u79cd\u573a\u666f\uff0c\u5e94\u8be5\u5728block\u4e2d\u5bf9 \u5bf9\u8c61\u4f7f\u7528__strong<\/code>\u4fee\u9970\uff0c\u4f7f\u5f97\u5728block\u671f\u95f4\u5bf9 \u5bf9\u8c61\u6301\u6709\uff0cblock\u6267\u884c\u7ed3\u675f\u540e\uff0c\u89e3\u9664\u5176\u6301\u6709\u3002<\/p> \n

\n
1<\/span> - (void<\/span>)testBlockRetainCycle {\r\n<\/span>2<\/span>     ClassA* objA = [[ClassA alloc] init];\r\n<\/span>3<\/span>     __weak typeof<\/span>(objA) weakObjA = objA;\r\n<\/span>4<\/span>     self.myBlock = ^() {\r\n<\/span>5<\/span>         __strong typeof<\/span>(weakObjA) strongWeakObjA = weakObjA;\r\n<\/span>6<\/span>         [strongWeakObjA doSomething];\r\n<\/span>7<\/span>     };\r\n<\/span>8<\/span>     objA.objA = self;\r\n<\/span>9<\/span> }<\/pre> \n   <\/div> \n   

 <\/p> \n

\u6ce8\uff1a\u6b64\u65b9\u6cd5\u53ea\u80fd\u4fdd\u8bc1\u5728block\u6267\u884c\u671f\u95f4\u5bf9\u8c61\u4e0d\u88ab\u91ca\u653e\uff0c\u5982\u679c\u5bf9\u8c61\u5728block\u6267\u884c\u6267\u884c\u4e4b\u524d\u5df2\u7ecf\u88ab\u91ca\u653e\u4e86\uff0c\u8be5\u65b9\u6cd5\u4e5f\u65e0\u6548\u3002<\/strong><\/p> \n <\/div> \n <\/div> \n

 <\/p> \n

\u53c2\u8003\u8d44\u6599\uff1ablock\u7684\u5faa\u73af\u5f15\u7528\u95ee\u9898<\/a><\/p> \n<\/div>","orderid":"0","title":"Block \u5faa\u73af\u5f15\u7528(\u4e2d)","smalltitle":"","mid":"0","fname":"Swift","special_id":"0","bak_id":"0","info":"0","hits":"330","pages":"1","comments":"0","posttime":"2019-09-03 02:41:09","list":"1567449669","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Block<\/A> \u5faa\u73af<\/A> \u5f15\u7528<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"120.229.33.54","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Block \u5faa\u73af\u5f15\u7528(\u4e2d)","lastview":"1714095369","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}