Created
November 2, 2017 06:17
-
-
Save yuchuanfeng/fab82018c69315426756955c124ab8b4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
#import <AFNetworking/AFNetworking.h> | |
@interface ViewController () | |
@end | |
static dispatch_queue_t _queue; | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view, typically from a nib. | |
_queue = dispatch_get_global_queue(0, 0); | |
} | |
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { | |
dispatch_async(_queue, ^{ | |
NSLog(@"start-----"); | |
[self getNetwork]; | |
NSLog(@"end-----"); | |
}); | |
} | |
- (void)getNetwork { | |
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; | |
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; | |
dispatch_semaphore_t semphore = dispatch_semaphore_create(0); | |
// request 1 | |
NSString* urlStr1 = @"http://www.weather.com.cn/data/sk/101010100.html"; | |
[manager GET:urlStr1 parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) { | |
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { | |
// NSDictionary* json = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:0 error:nil]; | |
NSLog(@"finishi-----------1"); | |
dispatch_semaphore_signal(semphore); | |
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { | |
}]; | |
// request 2 | |
NSString* urlStr2 = @"http://www.weather.com.cn/data/cityinfo/101010100.html"; | |
[manager GET:urlStr2 parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) { | |
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { | |
// NSDictionary* json = [NSJSONSerialization JSONObjectWithData:(NSData *)responseObject options:0 error:nil]; | |
NSLog(@"finishi-----------2"); | |
dispatch_semaphore_signal(semphore); | |
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { | |
}]; | |
long res1 = dispatch_semaphore_wait(semphore, DISPATCH_TIME_FOREVER); | |
NSLog(@"res1= %zd", res1); | |
long res2 = dispatch_semaphore_wait(semphore, DISPATCH_TIME_FOREVER); | |
NSLog(@"res2= %zd", res2); | |
} | |
@end | |
/* | |
2017-11-02 14:14:59.959984+0800 94-dispathc_sempore[2897:122353] start----- | |
2017-11-02 14:15:01.278140+0800 94-dispathc_sempore[2897:121208] finishi-----------1 | |
2017-11-02 14:15:01.278368+0800 94-dispathc_sempore[2897:122353] res1= 0 | |
2017-11-02 14:15:01.278511+0800 94-dispathc_sempore[2897:121208] finishi-----------2 | |
2017-11-02 14:15:01.278681+0800 94-dispathc_sempore[2897:122353] res2= 0 | |
2017-11-02 14:15:01.278855+0800 94-dispathc_sempore[2897:122353] end----- | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment