{"rsdb":{"rid":"409313","subhead":"","postdate":"0","aid":"282799","fid":"99","uid":"1","topic":"1","content":"
\n

\u968f\u4fbf\u8bf4\u8bf4<\/h2> \n

\u5176\u5b9eGCD\u5927\u5bb6\u90fd\u6709\u63a5\u89e6\u8fc7\uff0c\u4e5f\u4e0d\u5728\u89e3\u91caGCD\u662f\u4ec0\u4e48\uff0c\u4e3a\u4ec0\u4e48\u7a81\u7136\u60f3\u8bf4\u4fe1\u53f7\u91cf\u95ee\u9898\uff0c\u6700\u8fd1\u8fd9\u51e0\u6b21\u9762\u8bd5\uff0c\u5f53\u6211\u95ee\u5230\u9762\u8bd5\u8005\u600e\u4e48\u5904\u7406\u591a\u4e2a\u8bf7\u6c42\u5b8c\u6210\u540e\u7684\u4e00\u7cfb\u5217\u64cd\u4f5c\u65f6\uff0c\u6709\u7684\u8bf4\u9020\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\u7684\u505a\u8ffd\u52a0\uff0c\u5176\u5b9e\u8fd9\u6837\u53ef\u4ee5\uff0c\u4e5f\u7b97\u662f\u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u903b\u8f91\uff0c\u6709\u7684\u8bf4\u7528\u7ebf\u7a0b\u505a\u5ef6\u65f6\u64cd\u4f5c\uff0c\u600e\u4e48\u5ef6\u65f6\uff0c\u600e\u4e48\u64cd\u4f5c\u8bf4\u7684\u4e0d\u6e05\u695a\uff0c\u6709\u5c11\u90e8\u5206\u4f1a\u63d0\u5230GCD\u4fe1\u53f7\u91cf\uff0c\u4f46\u662f\u53ef\u80fd\u8bf4\u4e0d\u51fa\u6765\u600e\u4e48\u64cd\u4f5c\uff0c\u901a\u8fc7\u4fe1\u53f7\u91cf\u7684\u589e\u52a0\u4e0e\u9012\u51cf\uff0c\u8fdb\u884c\u7f51\u7edc\u7684\u5e76\u53d1\u8bf7\u6c42\uff0c\u6700\u540e\u518d\u505a\u7f51\u7edc\u8bf7\u6c42\u5b8c\u6210\u540e\u7684\u6700\u7ec8\u5904\u7406\uff1b\u5176\u5b9e\u5b9e\u9645\u4e0a\u5927\u5bb6\u5728\u505a\u7684\u65f6\u5019\uff0c\u5728\u7f51\u4e0a\u4e00\u641c\uff0c\u57fa\u672c\u90fd\u80fd\u627e\u5230\uff1b<\/span><\/p> \n

GCD\u4fe1\u53f7\u91cf\u7684\u5e94\u7528\u573a\u666f\uff0c\u4e00\u822c\u662f\u63a7\u5236\u6700\u5927\u5e76\u53d1\u91cf\uff0c\u63a7\u5236\u8d44\u6e90\u7684\u540c\u6b65\u8bbf\u95ee\uff0c\u5982\u6570\u636e\u8bbf\u95ee\uff0c\u7f51\u7edc\u540c\u6b65\u52a0\u8f7d\u7b49\u3002<\/span><\/p> \n

\u9700\u6c421\uff1a\u591a\u4e2a\u7f51\u7edc\u8bf7\u6c42\u5b8c\u6210\u540e\uff08\u65e0\u5e8f\uff09\u6267\u884c\u4e0b\u4e00\u6b65 <\/span><\/h2> \n

\u5148\u770b\u4e0b\u5982\u679c\u4e0d\u7528GCD\u7ebf\u7a0b\u7ec4\u6216\u4fe1\u53f7\u91cf\u4f1a\u600e\u4e48\u6267\u884c<\/span><\/p> \n

\n
- (void<\/span>)dispatchSyncSignal{\n    NSString <\/span>*urlString = @"<\/span>http:\/\/www.baidu.com<\/span>"<\/span>;\n    NSURL <\/span>*url = [NSURL URLWithString:urlString];\n    NSURLRequest <\/span>*request = [NSURLRequest requestWithURL:url];\n    NSURLSession <\/span>*session = [NSURLSession sharedSession];\n    \n    <\/span>for<\/span> (int<\/span> i=0<\/span>; i<5<\/span>; i++) {\n        NSURLSessionDataTask <\/span>*task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {\n            NSLog(<\/span>@"<\/span>\u8bf7\u6c42\u56de\u8c03 %d---%d<\/span>"<\/span>,i,i);\n            \n        }];\n        \n        [task resume];\n    }\n    NSLog(<\/span>@"<\/span>end<\/span>"<\/span>);\n}<\/span><\/pre> \n <\/div> \n 

\u8fd0\u884c\u540e\u540e\u53f0\u6253\u5370\u8f93\u51fa\uff1a<\/span><\/p> \n

\"\"   \"\" <\/p> \n

\u4ece\u4e0a\u9762\u4e24\u6b21\u6253\u5370\u7ed3\u679c\u770b\u51fa\uff0cend \u5148\u6267\u884c\uff0c\u7531\u4e8e\u7f51\u7edc\u8bf7\u6c42\u7684\u5f02\u6b65\u56de\u8c03\uff0c\u7136\u540e\u5404\u4e2a\u7f51\u7edc\u8bf7\u6c42\u7684\u56de\u8c03\u987a\u5e8f\u662f\u65e0\u5e8f\u7684\u3002\u4e0b\u9762\u9488\u5bf9\u9700\u6c42\u8fdb\u884c\u64cd\u4f5c\uff1b<\/span><\/p> \n

\u4f7f\u7528GCD\u7684\u7ebf\u7a0b\u7ec4 dispatch_group_t<\/strong><\/span><\/p> \n

\n
- (void<\/span>)dispatchSyncSignal1{ <\/span>\/\/<\/span>\u521b\u5efa\u7ebf\u7a0b\u7ec4<\/span>\n    dispatch_group_t group = dispatch_group_create(); NSString <\/span>*urlString = @"<\/span>http:\/\/www.baidu.com<\/span>"<\/span>; NSURL <\/span>*url = [NSURL URLWithString:urlString]; NSURLRequest <\/span>*request = [NSURLRequest requestWithURL:url]; NSURLSession <\/span>*session = [NSURLSession sharedSession]; <\/span>for<\/span> (int<\/span> i=0<\/span>; i<5<\/span>; i++) { dispatch_group_enter(group); NSURLSessionDataTask <\/span>*task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(<\/span>@"<\/span>\u8bf7\u6c42\u56de\u8c03 %d---%d<\/span>"<\/span>,i,i); dispatch_group_leave(group); }]; [task resume]; } dispatch_group_notify(group, dispatch_get_main_queue(), <\/span>^{ NSLog(<\/span>@"<\/span>end<\/span>"<\/span>); }); }<\/span><\/span><\/pre> \n <\/div> \n 

\u8fd0\u884c\u540e\u540e\u53f0\u6253\u5370\u8f93\u51fa\uff1a<\/span><\/p> \n

\"\"  \"\" <\/p> \n

\u4ece\u4e24\u6b21\u6253\u5370\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cend \u662f\u5728\u6240\u6709\u7f51\u7edc\u8bf7\u6c42\u4e4b\u540e\u624d\u8f93\u51fa\uff0c\u7b26\u5408\u4e86\u6211\u4eec\u7684\u9700\u6c42\u3002\u7136\u540e\u8bf4\u4e0b\u7528\u5230\u7684\u76f8\u5173\u65b9\u6cd5\uff1a<\/span><\/p> \n

dispatch_group_create(); \u521b\u5efa\u4e00\u4e2adispatch_group_t\uff1b<\/span><\/p> \n

dispatch_group_enter(); \u6bcf\u6b21\u7f51\u7edc\u8bf7\u6c42\u524d\u8c03\u7528\uff1b<\/span><\/p> \n

dispatch_group_leave(); \u6bcf\u6b21\u7f51\u7edc\u8bf7\u6c42\u540e\u8c03\u7528\uff1b<\/span><\/p> \n

dispatch_group_enter(); \u548c dispatch_group_leave(); \u5fc5\u987b\u914d\u5408\u4f7f\u7528\uff0c\u6709\u51e0\u6b21enter\u5c31\u8981\u6709\u51e0\u6b21leave\uff1b<\/span><\/p> \n

\u7136\u540e\u5f53\u6240\u6709dispatch_group_enter(); \u7684 block \u90fd dispatch_group_leave(); \u540e\uff0c\u4f1a\u6267\u884cdispatch_group_notify\u7684block\u3002<\/span><\/p> \n

\u4f7f\u7528GCD\u7684\u4fe1\u53f7\u91cf semaphore_t<\/strong><\/span><\/p> \n

\n
- (void<\/span>)dispatchSyncSignal2{ dispatch_semaphore_t semaphore <\/span>= dispatch_semaphore_create(0<\/span>); NSString <\/span>*urlString = @"<\/span>http:\/\/www.baidu.com<\/span>"<\/span>; NSURL <\/span>*url = [NSURL URLWithString:urlString]; NSURLRequest <\/span>*request = [NSURLRequest requestWithURL:url]; NSURLSession <\/span>*session = [NSURLSession sharedSession]; __block NSInteger count <\/span>= 0<\/span>; <\/span>for<\/span> (int<\/span> i=0<\/span>; i<5<\/span>; i++) { NSURLSessionDataTask <\/span>*task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(<\/span>@"<\/span>\u8bf7\u6c42\u56de\u8c03 %d---%d<\/span>"<\/span>,i,i); count <\/span>= count + 1<\/span>; <\/span>if<\/span> (count == 5<\/span>) { dispatch_semaphore_signal(semaphore); count <\/span>= 0<\/span>; } }]; [task resume]; } dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); dispatch_async(dispatch_get_main_queue(), <\/span>^{ NSLog(<\/span>@"<\/span>end<\/span>"<\/span>); }); }<\/span><\/span><\/pre> \n <\/div> \n 

\u8fd0\u884c\u540e\u540e\u53f0\u6253\u5370\u8f93\u51fa\uff1a<\/span><\/p> \n

\"\"  \"\"<\/p> \n

\u4ece\u4e24\u6b21\u6253\u5370\u8f93\u51fa\u7ed3\u679c\u53ef\u4ee5\u770b\u51fa\uff0cend \u4e5f\u662f\u5728\u6240\u6709\u7f51\u7edc\u8bf7\u6c42\u4e4b\u540e\u624d\u8f93\u51fa\uff0c\u4e5f\u7b26\u5408\u4e86\u6211\u4eec\u7684\u9700\u6c42\u3002\u7136\u540e\u8bf4\u4e0b\u7528\u5230\u7684\u76f8\u5173\u65b9\u6cd5\uff1a<\/span><\/p> \n

\n

dispatch_semaphore \u4fe1\u53f7\u91cf\u5982\u679c\u8ba1\u6570\u4e3a0\uff0c\u5219\u7b49\u5f85\u3002dispatch_semaphore_signal(semaphore)\u4e3a\u8ba1\u6570+1\uff0cdispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER)\u4e3a\u8bbe","orderid":"0","title":"iOS-\u5173\u4e8eGCD\u4fe1\u53f7\u91cf\u90a3\u4e9b\u4e8b\u513f(\u4e00)","smalltitle":"","mid":"0","fname":"iOS","special_id":"0","bak_id":"0","info":"0","hits":"124","pages":"3","comments":"0","posttime":"2020-03-31 18:13:52","list":"1585649632","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":"iOS-<\/A> \u5173\u4e8e<\/A> GCD<\/A> \u4fe1\u53f7<\/A> \u90a3\u4e9b<\/A> \u4e8b\u513f<\/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":"14.17.22.33","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"iOS-\u5173\u4e8eGCD\u4fe1\u53f7\u91cf\u90a3\u4e9b\u4e8b\u513f","lastview":"1590393163","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}