-
Notifications
You must be signed in to change notification settings - Fork 0
/
HFConnectionManager.m
72 lines (55 loc) · 1.54 KB
/
HFConnectionManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// DownladerManager.m
// MultDownloader
//
// Created by LiQiu Yu on 15-9-28.
// Copyright (c) 2015年 LiQiu Yu. All rights reserved.
//
#import "HFConnectionManager.h"
#import "HFConnectionOpertation.h"
@interface HFConnectionManager ()
@end
@implementation HFConnectionManager
static HFConnectionManager *instance;
+ (HFConnectionManager *)defaultManager {
if (!instance) {
instance = [[self alloc] init];
}
return instance;
}
- (id)init {
if (self = [super init]) {
downloadQueue = [[NSOperationQueue alloc] init];
downloadQueue.maxConcurrentOperationCount = MAX_CONCURRENT_OPERATION_COUNT;
i = 0;
}
return self;
}
- (void)dealloc{
#if !__has_feature(objc_arc)
[downloadQueue release];
[super dealloc];
#endif
}
- (void)setMaxConcurrentOperationCount:(int)max{
if (max >0) {
downloadQueue.maxConcurrentOperationCount = max;
}
}
- (void)addRequest:(HFRequest *)reqeust {
HFConnectionOpertation *downloader = LW_AUTORELEASE([[HFConnectionOpertation alloc] initWithRequest:reqeust]);
[downloadQueue addOperation:downloader];
i ++;
}
- (HFResponse *)addSynchronizedReqeust:(HFRequest *)request{
NSError *error;
NSURLResponse *response;
NSData *data= [NSURLConnection sendSynchronousRequest:request.request returningResponse:&response error:&error];
HFResponse *re = [[HFResponse alloc]init];
re.response = response;
re.responseData = data;
re.error = error;
re.responseTarget = request.requestTarget;
return re;
}
@end