Skip to content

Instantly share code, notes, and snippets.

@yuchuanfeng
Created November 2, 2017 07:09
Show Gist options
  • Save yuchuanfeng/50a15542d5d990bee471c3bad6fc12e4 to your computer and use it in GitHub Desktop.
Save yuchuanfeng/50a15542d5d990bee471c3bad6fc12e4 to your computer and use it in GitHub Desktop.
#import "ViewController.h"
#import <AFNetworking/AFNetworking.h>
@interface ViewController ()
@end
static dispatch_queue_t _queue;
static dispatch_semaphore_t _semphore;
@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, ^{
_semphore = dispatch_semaphore_create(1);
for (int i = 0; i<10; i++) {
dispatch_semaphore_wait(_semphore, DISPATCH_TIME_FOREVER);
NSLog(@"start-----%zd", i);
[self getNetwork: i];
NSLog(@"end-----%zd", i);
}
});
}
- (void)getNetwork: (int) i {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// 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-----------%zd", i);
dispatch_semaphore_signal(_semphore);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
@end
/*
2017-11-02 15:07:48.134864+0800 94-dispathc_sempore[3114:153398] start-----0
2017-11-02 15:07:48.140954+0800 94-dispathc_sempore[3114:153398] end-----0
2017-11-02 15:07:48.691577+0800 94-dispathc_sempore[3114:153334] finishi-----------0
2017-11-02 15:07:48.691824+0800 94-dispathc_sempore[3114:153398] start-----1
2017-11-02 15:07:48.693197+0800 94-dispathc_sempore[3114:153398] end-----1
2017-11-02 15:07:48.783829+0800 94-dispathc_sempore[3114:153334] finishi-----------1
2017-11-02 15:07:48.784099+0800 94-dispathc_sempore[3114:153398] start-----2
2017-11-02 15:07:48.785084+0800 94-dispathc_sempore[3114:153398] end-----2
2017-11-02 15:07:48.954711+0800 94-dispathc_sempore[3114:153334] finishi-----------2
2017-11-02 15:07:48.954988+0800 94-dispathc_sempore[3114:153398] start-----3
2017-11-02 15:07:48.955980+0800 94-dispathc_sempore[3114:153398] end-----3
2017-11-02 15:07:49.140861+0800 94-dispathc_sempore[3114:153334] finishi-----------3
2017-11-02 15:07:49.141151+0800 94-dispathc_sempore[3114:153398] start-----4
2017-11-02 15:07:49.142115+0800 94-dispathc_sempore[3114:153398] end-----4
2017-11-02 15:07:49.394020+0800 94-dispathc_sempore[3114:153334] finishi-----------4
2017-11-02 15:07:49.394247+0800 94-dispathc_sempore[3114:153398] start-----5
2017-11-02 15:07:49.395194+0800 94-dispathc_sempore[3114:153398] end-----5
2017-11-02 15:07:49.531068+0800 94-dispathc_sempore[3114:153334] finishi-----------5
2017-11-02 15:07:49.531296+0800 94-dispathc_sempore[3114:153398] start-----6
2017-11-02 15:07:49.532154+0800 94-dispathc_sempore[3114:153398] end-----6
2017-11-02 15:07:49.599365+0800 94-dispathc_sempore[3114:153334] finishi-----------6
2017-11-02 15:07:49.599544+0800 94-dispathc_sempore[3114:153398] start-----7
2017-11-02 15:07:49.600254+0800 94-dispathc_sempore[3114:153398] end-----7
2017-11-02 15:07:49.662924+0800 94-dispathc_sempore[3114:153334] finishi-----------7
2017-11-02 15:07:49.663210+0800 94-dispathc_sempore[3114:153398] start-----8
2017-11-02 15:07:49.664253+0800 94-dispathc_sempore[3114:153398] end-----8
2017-11-02 15:07:49.753110+0800 94-dispathc_sempore[3114:153334] finishi-----------8
2017-11-02 15:07:49.753335+0800 94-dispathc_sempore[3114:153398] start-----9
2017-11-02 15:07:49.754259+0800 94-dispathc_sempore[3114:153398] end-----9
2017-11-02 15:07:49.898029+0800 94-dispathc_sempore[3114:153334] finishi-----------9
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment