12. 다중 스레드 GCD 스레드 그룹과 GCD 신호량이 비동기 스레드를 동기 함수로 전환

4241 단어
1. 부딪히는 문제:
             ,          ,                             ,    ,               .

실행 코드:
#import "ViewController.h"
 2 #import 
 3 
 4 
 5 @interface ViewController ()
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     [self getNetworkingData];
14 }
15 
16 - (void)getNetworkingData{
17     NSString *appIdKey = @"8781e4ef1c73ff20a180d3d7a42a8c04";
18     NSString* urlString_1 = @"http://api.openweathermap.org/data/2.5/weather";
19     NSString* urlString_2 = @"http://api.openweathermap.org/data/2.5/forecast/daily";
20     NSDictionary* dictionary =@{@"lat":@"40.04991291",
21                                 @"lon":@"116.25626162",
22                                 @"APPID" : appIdKey};
23     //    
24     dispatch_group_t group = dispatch_group_create();
25     //                
26     dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
27         //      
28         dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
29         //         
30         AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
31         [manager GET:urlString_1
32           parameters:dictionary
33             progress:nil
34              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
35                  NSLog(@"      1:%@",[responseObject class]);
36                  //       ,     
37                  dispatch_semaphore_signal(semaphore);
38              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
39                  NSLog(@"      ");
40                  //       ,      
41                  dispatch_semaphore_signal(semaphore);
42              }];
43         //            ,      
44         dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
45     });
46     //                
47     dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
48         //      
49         dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
50         //         
51         AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
52         [manager GET:urlString_2
53           parameters:dictionary
54             progress:nil
55              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
56                  NSLog(@"      2:%@",[responseObject class]);
57                  //       ,     
58                  dispatch_semaphore_signal(semaphore);
59              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
60                  NSLog(@"      ");
61                  //       ,      
62                  dispatch_semaphore_signal(semaphore);
63              }];
64         //            ,      
65         dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
66     });
67     dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
68         NSLog(@"       ,              。");
69     });
70 }
71 
72 @end

인쇄 결과:
2016-03-15 04:01:53.279 NetWorking[83611:1508240]       1:__NSCFDictionary
2016-03-15 04:01:53.280 NetWorking[83611:1508240]       2:__NSCFDictionary
2016-03-15 04:01:53.281 NetWorking[83611:1508287]        ,              。

3. 요약:
                   ,              ,                                             。        (          )       ,      ,                   ,                  ,           ,               ,              。                             ,            block(  48~53       block)       ,          block          。

       block          ,     ,       block             AFN       ,AFN     ,          ,      ,  ,        ,            ,AFN         :

4. 요약:
          ,              ,          ,       。

       AFN       ,block    main       , get                  。      ,               main     block         。            wait,main      signal     

좋은 웹페이지 즐겨찾기