Skip to content

Commit d112a06

Browse files
committed
Merge pull request #12 from longbai/update_readme
修正httpmanager 生命周期问题
2 parents 6909cd2 + 739170a commit d112a06

File tree

10 files changed

+35
-33
lines changed

10 files changed

+35
-33
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#Changelog
22

3+
## 7.0.5 (2014-10-28)
4+
5+
### 修正
6+
* ResumeUpload中httpManager weak引用造成nil
7+
* 重构代码,更符合objc 现代方式
8+
9+
310
## 7.0.4 (2014-10-17)
411

512
### 增加

Qiniu.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'Qiniu'
3-
s.version = '7.0.4'
3+
s.version = '7.0.5'
44
s.summary = 'Qiniu Resource Storage SDK for iOS and Mac'
55
s.homepage = 'https://github.com/qiniu/objc-sdk'
66
s.social_media_url = 'http://weibo.com/qiniutek'

QiniuSDK/Common/QNVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99
#import <Foundation/Foundation.h>
1010

11-
static const NSString *kQiniuVersion = @"7.0.4";
11+
static const NSString *kQiniuVersion = @"7.0.5";

QiniuSDK/Http/QNHttpManager.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ @interface QNHttpManager ()
1616
@property (nonatomic) AFHTTPRequestOperationManager *httpManager;
1717
@end
1818

19+
static NSString *userAgent = nil;
20+
1921
@implementation QNHttpManager
2022

23+
+ (void)initialize {
24+
userAgent = QNUserAgent();
25+
}
26+
2127
- (instancetype)init {
2228
if (self = [super init]) {
2329
_httpManager = [[AFHTTPRequestOperationManager alloc] init];
@@ -69,7 +75,7 @@ - (void) sendRequest:(NSMutableURLRequest *)request
6975
}];
7076
}
7177

72-
[request setValue:QNUserAgent() forHTTPHeaderField:@"User-Agent"];
78+
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
7379
[request setValue:nil forHTTPHeaderField:@"Accept-Language"];
7480
[_httpManager.operationQueue addOperation:operation];
7581
}

QiniuSDK/Http/QNUserAgent.m

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,13 @@
2020
static NSString *clientId(void) {
2121
long long now_timestamp = [[NSDate date] timeIntervalSince1970] * 1000;
2222
int r = arc4random() % 1000;
23-
2423
return [NSString stringWithFormat:@"%lld%u", now_timestamp, r];
2524
}
2625

27-
static NSString *_userAgent = nil;
28-
2926
NSString *QNUserAgent(void) {
30-
if (_userAgent) {
31-
return [_userAgent copy];
32-
}
33-
3427
#if __IPHONE_OS_VERSION_MIN_REQUIRED
35-
_userAgent = [NSString stringWithFormat:@"QiniuObject-C/%@ (%@; iOS %@; %@)", kQiniuVersion, [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], clientId()];
28+
return [NSString stringWithFormat:@"QiniuObject-C/%@ (%@; iOS %@; %@)", kQiniuVersion, [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemVersion], clientId()];
3629
#else
37-
_userAgent = [NSString stringWithFormat:@"QiniuObject-C/%@ (Mac OS X %@; %@)", kQiniuVersion, [[NSProcessInfo processInfo] operatingSystemVersionString], clientId()];
30+
return [NSString stringWithFormat:@"QiniuObject-C/%@ (Mac OS X %@; %@)", kQiniuVersion, [[NSProcessInfo processInfo] operatingSystemVersionString], clientId()];
3831
#endif
39-
return [_userAgent copy];
4032
}

QiniuSDK/Storage/QNResumeUpload.m

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
@interface QNResumeUpload ()
2222

2323
@property (nonatomic, strong) NSData *data;
24-
@property (nonatomic, weak) QNHttpManager *httpManager;
24+
@property (nonatomic, strong) QNHttpManager *httpManager;
2525
@property UInt32 size;
2626
@property (nonatomic) int retryTimes;
2727
@property (nonatomic, strong) NSString *key;
@@ -32,8 +32,8 @@ @interface QNResumeUpload ()
3232
@property (nonatomic, strong) NSMutableArray *contexts;
3333
@property (nonatomic, readonly, getter = isCancelled) BOOL cancelled;
3434

35-
@property UInt64 modifyTime;
36-
@property (nonatomic, weak) id <QNRecorderDelegate> recorder;
35+
@property int64_t modifyTime;
36+
@property (nonatomic, strong) id <QNRecorderDelegate> recorder;
3737

3838
@property UInt32 chunkCrc;
3939

@@ -293,18 +293,17 @@ - (void)makeFile:(NSString *)uphost
293293
mime = [[NSString alloc] initWithFormat:@"/mimetype/%@", [QNUrlSafeBase64 encodeString:self.option.mimeType]];
294294
}
295295

296-
NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkfile/%u%@", uphost, (unsigned int)self.size, mime];
296+
__block NSString *url = [[NSString alloc] initWithFormat:@"http://%@/mkfile/%u%@", uphost, (unsigned int)self.size, mime];
297297

298298
if (self.key != nil) {
299299
NSString *keyStr = [[NSString alloc] initWithFormat:@"/key/%@", [QNUrlSafeBase64 encodeString:self.key]];
300300
url = [NSString stringWithFormat:@"%@%@", url, keyStr];
301301
}
302302

303303
if (self.option && self.option.params) {
304-
NSEnumerator *e = [self.option.params keyEnumerator];
305-
for (id key = [e nextObject]; key != nil; key = [e nextObject]) {
306-
url = [NSString stringWithFormat:@"%@/%@/%@", url, key, [QNUrlSafeBase64 encodeString:(self.option.params)[key]]];
307-
}
304+
[self.option.params enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSString *obj, BOOL *stop) {
305+
url = [NSString stringWithFormat:@"%@/%@/%@", url, key, [QNUrlSafeBase64 encodeString:obj]];
306+
}];
308307
}
309308

310309
NSMutableData *postData = [NSMutableData data];

QiniuSDK/Storage/QNUploadOption.m

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ + (NSDictionary *)filteParam:(NSDictionary *)params {
2323
return nil;
2424
}
2525
NSMutableDictionary *ret = [NSMutableDictionary dictionary];
26-
@autoreleasepool {
27-
NSEnumerator *e = [params keyEnumerator];
28-
for (NSString *key = [e nextObject]; key != nil; key = [e nextObject]) {
29-
if ([key hasPrefix:@"x:"]) {
30-
id val = params[key];
31-
if (val != nil) {
32-
ret[key] = params[key];
33-
}
34-
}
26+
27+
[params enumerateKeysAndObjectsUsingBlock: ^(NSString *key, NSString *obj, BOOL *stop) {
28+
if ([key hasPrefix:@"x:"]) {
29+
ret[key] = obj;
3530
}
36-
}
31+
}];
32+
3733
return ret;
3834
}
3935

QiniuSDKTests/QNFileRecorderTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ - (void)setUp {
3131
_upManager = [[QNUploadManager alloc] initWithRecorder:file
3232
];
3333
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
34-
NSString *travis = [[[NSProcessInfo processInfo]environment]objectForKey:@"QINIU_TEST_ENV"];
34+
NSString *travis = [[NSProcessInfo processInfo]environment][@"QINIU_TEST_ENV"];
3535
if ([travis isEqualToString:@"travis"]) {
3636
_inTravis = YES;
3737
}

QiniuSDKTests/QNResumeUploadTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ - (void)setUp {
2626
[super setUp];
2727
_upManager = [[QNUploadManager alloc] init];
2828
#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
29-
NSString *travis = [[[NSProcessInfo processInfo]environment]objectForKey:@"QINIU_TEST_ENV"];
29+
NSString *travis = [[NSProcessInfo processInfo]environment][@"QINIU_TEST_ENV"];
3030
if ([travis isEqualToString:@"travis"]) {
3131
_inTravis = YES;
3232
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pod "Qiniu", "~> 7.0"
3636
...
3737
```
3838
39+
建议 QNUploadManager 创建一次重复使用,或者使用单例方式创建。
40+
3941
## 测试
4042
4143
``` bash

0 commit comments

Comments
 (0)