Skip to content

Commit 7caf4c9

Browse files
authored
Merge pull request #397 from YangSen-qn/develop
优化
2 parents 2085864 + 4cc83eb commit 7caf4c9

26 files changed

+415
-116
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
#Changelog
2+
## 8.3.1(2021-08-11)
3+
## 优化
4+
- dns 增加最大有效时间
5+
- 网络检测可关闭
6+
- 增加速度统计
7+
28
## 8.3.0(2021-07-20)
39
## 增加
410
- 支持 InputStream 上传

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 = '8.3.0'
3+
s.version = '8.3.1'
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'

QiniuDemo/QiniuDemo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
31304CA525628E97001034D2 /* image.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 31304CA225628E97001034D2 /* image.jpg */; };
1515
3152B913268C4EC3004705AA /* UploadResource_1.44G.zip in Resources */ = {isa = PBXBuildFile; fileRef = 3152B912268C4EC3004705AA /* UploadResource_1.44G.zip */; };
1616
318191AB2531516F00D18536 /* UploadResource.dmg in Resources */ = {isa = PBXBuildFile; fileRef = 318191AA2531516F00D18536 /* UploadResource.dmg */; };
17+
31B188E426C365F0006046CC /* UploadResource_6M.zip in Resources */ = {isa = PBXBuildFile; fileRef = 31B188E326C365F0006046CC /* UploadResource_6M.zip */; };
1718
93D230241C86D7F700434F6D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230231C86D7F700434F6D /* main.m */; };
1819
93D230271C86D7F700434F6D /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230261C86D7F700434F6D /* AppDelegate.m */; };
1920
93D2302A1C86D7F700434F6D /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230291C86D7F700434F6D /* ViewController.m */; };
@@ -51,6 +52,7 @@
5152
318191AA2531516F00D18536 /* UploadResource.dmg */ = {isa = PBXFileReference; lastKnownFileType = file; path = UploadResource.dmg; sourceTree = "<group>"; };
5253
318191AF25315F3500D18536 /* Configure.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Configure.h; sourceTree = "<group>"; };
5354
3189882026469145003CCA68 /* QiniuDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = QiniuDemo.entitlements; sourceTree = "<group>"; };
55+
31B188E326C365F0006046CC /* UploadResource_6M.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = UploadResource_6M.zip; sourceTree = "<group>"; };
5456
6B9BC5A8D93F988C1240E05F /* libPods-QiniuDemo.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-QiniuDemo.a"; sourceTree = BUILT_PRODUCTS_DIR; };
5557
93D2301F1C86D7F700434F6D /* QiniuDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QiniuDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
5658
93D230231C86D7F700434F6D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -103,6 +105,7 @@
103105
318191A92531516F00D18536 /* DemoTestResource */ = {
104106
isa = PBXGroup;
105107
children = (
108+
31B188E326C365F0006046CC /* UploadResource_6M.zip */,
106109
3152B912268C4EC3004705AA /* UploadResource_1.44G.zip */,
107110
31304CA225628E97001034D2 /* image.jpg */,
108111
31304CA025628E97001034D2 /* image.png */,
@@ -311,6 +314,7 @@
311314
3152B913268C4EC3004705AA /* UploadResource_1.44G.zip in Resources */,
312315
93D2302D1C86D7F700434F6D /* Main.storyboard in Resources */,
313316
31304CA525628E97001034D2 /* image.jpg in Resources */,
317+
31B188E426C365F0006046CC /* UploadResource_6M.zip in Resources */,
314318
31304CA325628E97001034D2 /* image.png in Resources */,
315319
31304CA425628E97001034D2 /* UploadResource_49M.zip in Resources */,
316320
);

QiniuDemo/QiniuDemo/ViewController.m

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ - (IBAction)uploadAction:(UIButton *)sender {
5050

5151
#ifdef YourToken
5252
NSString *path = [[NSBundle mainBundle] pathForResource:@"UploadResource.dmg" ofType:nil];
53-
path = [[NSBundle mainBundle] pathForResource:@"UploadResource_49M.zip" ofType:nil];
54-
path = [[NSBundle mainBundle] pathForResource:@"UploadResource_1.44G.zip" ofType:nil];
53+
path = [[NSBundle mainBundle] pathForResource:@"image.png" ofType:nil];
54+
path = [[NSBundle mainBundle] pathForResource:@"image.jpg" ofType:nil];
55+
path = [[NSBundle mainBundle] pathForResource:@"UploadResource_6M.zip" ofType:nil];
56+
// path = [[NSBundle mainBundle] pathForResource:@"UploadResource_1.44G.zip" ofType:nil];
5557

5658
// NSFileManager *manager = [NSFileManager defaultManager];
5759
// NSURL *desktopUrl = [manager URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask].firstObject;
@@ -90,10 +92,14 @@ - (void)changeUploadState:(UploadState)uploadState{
9092

9193
- (void)uploadImageToQNFilePath:(NSString *)filePath {
9294

95+
// kQNGlobalConfiguration.isDnsOpen = false;
96+
97+
98+
NSString *key = [NSString stringWithFormat:@"iOS_Demo_%@", [NSDate date]];
9399
self.token = YourToken;
94100
QNConfiguration *configuration = [QNConfiguration build:^(QNConfigurationBuilder *builder) {
95-
builder.useConcurrentResumeUpload = NO;
96-
builder.resumeUploadVersion = QNResumeUploadVersionV1;
101+
builder.useConcurrentResumeUpload = true;
102+
builder.resumeUploadVersion = QNResumeUploadVersionV2;
97103
builder.recorder = [QNFileRecorder fileRecorderWithFolder:[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] error:nil];
98104
}];
99105
QNUploadManager *upManager = [[QNUploadManager alloc] initWithConfiguration:configuration];
@@ -120,7 +126,7 @@ - (void)uploadImageToQNFilePath:(NSString *)filePath {
120126

121127
long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];
122128
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
123-
[upManager putInputStream:stream sourceId:filePath.lastPathComponent size:fileSize fileName:filePath.lastPathComponent key:@"DemoResource_1.44G" token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
129+
[upManager putInputStream:stream sourceId:filePath.lastPathComponent size:fileSize fileName:filePath.lastPathComponent key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
124130
NSLog(@"info ===== %@", info);
125131
NSLog(@"resp ===== %@", resp);
126132

@@ -130,8 +136,8 @@ - (void)uploadImageToQNFilePath:(NSString *)filePath {
130136

131137
// NSURL *url = [NSURL fileURLWithPath:filePath];
132138
// PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
133-
// PHAsset *asset = fetchResult.firstObject;
134-
// [upManager putPHAsset:asset key:@"DemoResource" token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
139+
// PHAsset *asset = [self getPHAssert];
140+
// [upManager putPHAsset:asset key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
135141
// NSLog(@"info ===== %@", info);
136142
// NSLog(@"resp ===== %@", resp);
137143
//
@@ -141,6 +147,41 @@ - (void)uploadImageToQNFilePath:(NSString *)filePath {
141147
// option:uploadOption];
142148
}
143149

150+
- (PHAsset *)getPHAssert {
151+
152+
PHFetchOptions *option = [[PHFetchOptions alloc] init];
153+
option.predicate = [NSPredicate predicateWithFormat:@"mediaType == %ld",PHAssetMediaTypeVideo];
154+
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
155+
156+
__block PHAsset *phAsset = nil;
157+
//fetchAssetCollectionsWithType
158+
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
159+
for (PHAssetCollection *collection in smartAlbums) {
160+
// 有可能是PHCollectionList类的的对象,过滤掉
161+
if (![collection isKindOfClass:[PHAssetCollection class]]) continue;
162+
// 过滤空相册
163+
if (collection.estimatedAssetCount <= 0) continue;
164+
165+
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:option];
166+
167+
[fetchResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
168+
169+
phAsset = (PHAsset *)obj;
170+
//可通过此PHAsset用下边方法分别获取时常、地址及缩略图
171+
172+
if (phAsset) {
173+
*stop = true;
174+
}
175+
}];
176+
177+
if (phAsset) {
178+
break;
179+
}
180+
}
181+
182+
return phAsset;
183+
}
184+
144185
- (void)gotoImageLibrary {
145186
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
146187
UIImagePickerController *picker = [[UIImagePickerController alloc] init];

QiniuSDK/Collect/QNReportItem.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ extern NSString *const QNReportRequestKeyPrefetchedDnsSource;
9090
extern NSString *const QNReportRequestKeyPrefetchedBefore;
9191
extern NSString *const QNReportRequestKeyPrefetchedErrorMessage;
9292
extern NSString *const QNReportRequestKeyNetworkMeasuring;
93-
93+
extern NSString *const QNReportRequestKeyPerceptiveSpeed;
9494

9595
//MARK:-- 分块上传统计⽇志
9696
extern NSString *const QNReportBlockKeyLogType;
@@ -111,6 +111,7 @@ extern NSString *const QNReportBlockKeyOsName;
111111
extern NSString *const QNReportBlockKeyOsVersion;
112112
extern NSString *const QNReportBlockKeySDKName;
113113
extern NSString *const QNReportBlockKeySDKVersion;
114+
extern NSString *const QNReportBlockKeyPerceptiveSpeed;
114115

115116
//MARK:-- 上传质量统计
116117
extern NSString *const QNReportQualityKeyLogType;
@@ -122,13 +123,14 @@ extern NSString *const QNReportQualityKeyTotalElapsedTime;
122123
extern NSString *const QNReportQualityKeyRequestsCount;
123124
extern NSString *const QNReportQualityKeyRegionsCount;
124125
extern NSString *const QNReportQualityKeyBytesSent;
126+
extern NSString *const QNReportQualityKeyFileSize;
125127
extern NSString *const QNReportQualityKeyCloudType;
126128
extern NSString *const QNReportQualityKeyErrorType;
127129
extern NSString *const QNReportQualityKeyErrorDescription;
128130
extern NSString *const QNReportQualityKeyOsName;
129131
extern NSString *const QNReportQualityKeyOsVersion;
130132
extern NSString *const QNReportQualityKeySDKName;
131133
extern NSString *const QNReportQualityKeySDKVersion;
132-
134+
extern NSString *const QNReportQualityKeyPerceptiveSpeed;
133135

134136
NS_ASSUME_NONNULL_END

QiniuSDK/Collect/QNReportItem.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ - (NSString *)qualityResult{
204204
NSString * const QNReportRequestKeyPrefetchedBefore = @"prefetched_before";
205205
NSString * const QNReportRequestKeyPrefetchedErrorMessage = @"prefetched_error_message";
206206
NSString * const QNReportRequestKeyNetworkMeasuring = @"network_measuring";
207+
NSString * const QNReportRequestKeyPerceptiveSpeed = @"perceptive_speed";
207208

208209
//MARK:-- 分块上传统计⽇志
209210
NSString * const QNReportBlockKeyLogType = @"log_type";
@@ -224,6 +225,7 @@ - (NSString *)qualityResult{
224225
NSString * const QNReportBlockKeyOsVersion = @"os_version";
225226
NSString * const QNReportBlockKeySDKName = @"sdk_name";
226227
NSString * const QNReportBlockKeySDKVersion = @"sdk_version";
228+
NSString * const QNReportBlockKeyPerceptiveSpeed = @"perceptive_speed";
227229

228230

229231
//MARK:-- 上传质量统计
@@ -236,10 +238,12 @@ - (NSString *)qualityResult{
236238
NSString * const QNReportQualityKeyRequestsCount = @"requests_count";
237239
NSString * const QNReportQualityKeyRegionsCount = @"regions_count";
238240
NSString * const QNReportQualityKeyBytesSent = @"bytes_sent";
241+
NSString * const QNReportQualityKeyFileSize = @"file_size";
239242
NSString * const QNReportQualityKeyCloudType = @"cloud_type";
240243
NSString * const QNReportQualityKeyErrorType = @"error_type";
241244
NSString * const QNReportQualityKeyErrorDescription = @"error_description";
242245
NSString * const QNReportQualityKeyOsName = @"os_name";
243246
NSString * const QNReportQualityKeyOsVersion = @"os_version";
244247
NSString * const QNReportQualityKeySDKName = @"sdk_name";
245248
NSString * const QNReportQualityKeySDKVersion = @"sdk_version";
249+
NSString * const QNReportQualityKeyPerceptiveSpeed = @"perceptive_speed";

QiniuSDK/Http/ConnectCheck/QNConnectChecker.m

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ + (QNSingleFlight *)singleFlight {
2727
return singleFlight;
2828
}
2929

30+
+ (dispatch_queue_t)checkQueue {
31+
static dispatch_queue_t queue;
32+
static dispatch_once_t onceToken;
33+
dispatch_once(&onceToken, ^{
34+
queue = dispatch_queue_create("com.qiniu.NetworkCheckQueue", DISPATCH_QUEUE_CONCURRENT);
35+
});
36+
return queue;
37+
}
38+
3039
+ (BOOL)isConnected:(QNUploadSingleRequestMetrics *)metrics {
3140
return metrics && ((NSHTTPURLResponse *)metrics.response).statusCode > 99;
3241
}
@@ -65,7 +74,6 @@ + (void)checkAllHosts:(void (^)(QNUploadSingleRequestMetrics *metrics))complete
6574

6675
__block int completeCount = 0;
6776
__block BOOL isCompleted = false;
68-
__block BOOL isConnected = false;
6977
kQNWeakSelf;
7078
NSArray *allHosts = [kQNGlobalConfiguration.connectCheckURLStrings copy];
7179
for (NSString *host in allHosts) {
@@ -76,9 +84,6 @@ + (void)checkAllHosts:(void (^)(QNUploadSingleRequestMetrics *metrics))complete
7684
@synchronized (self) {
7785
completeCount += 1;
7886
}
79-
if (isHostConnected) {
80-
isConnected = YES;
81-
}
8287
if (isHostConnected || completeCount == allHosts.count) {
8388
@synchronized (self) {
8489
if (isCompleted) {
@@ -104,8 +109,30 @@ + (void)checkHost:(NSString *)host complete:(void (^)(QNUploadSingleRequestMetri
104109
request.HTTPMethod = @"HEAD";
105110
request.timeoutInterval = kQNGlobalConfiguration.connectCheckTimeout;
106111

112+
__block BOOL hasCallback = false;
113+
114+
QNUploadSingleRequestMetrics *timeoutMetric = [QNUploadSingleRequestMetrics emptyMetrics];
115+
[timeoutMetric start];
116+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * kQNGlobalConfiguration.connectCheckTimeout), [self checkQueue], ^{
117+
@synchronized (self) {
118+
if (hasCallback) {
119+
return;
120+
}
121+
hasCallback = true;
122+
}
123+
[timeoutMetric end];
124+
timeoutMetric.error = [NSError errorWithDomain:@"com.qiniu.NetworkCheck" code:NSURLErrorTimedOut userInfo:nil];
125+
complete(timeoutMetric);
126+
});
127+
107128
QNUploadSystemClient *client = [[QNUploadSystemClient alloc] init];
108129
[client request:request connectionProxy:nil progress:nil complete:^(NSURLResponse *response, QNUploadSingleRequestMetrics * metrics, NSData * _Nullable data, NSError * error) {
130+
@synchronized (self) {
131+
if (hasCallback) {
132+
return;
133+
}
134+
hasCallback = true;
135+
}
109136
QNLogInfo(@"== checkHost:%@ responseInfo:%@", host, response);
110137
complete(metrics);
111138
}];

QiniuSDK/Http/Dns/QNDnsPrefetch.m

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,22 @@ + (instancetype)inetAddress:(id)addressInfo{
9090
}
9191
}
9292

93+
/// 过了 ttl 时间则需要刷新
94+
- (BOOL)needRefresh{
95+
if (!self.timestampValue || !self.ipValue || self.ipValue.length == 0) {
96+
return NO;
97+
}
98+
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
99+
return currentTimestamp > (self.timestampValue.doubleValue + self.ttlValue.doubleValue);
100+
}
101+
102+
/// 只要在最大 ttl 时间内,即为有效
93103
- (BOOL)isValid{
94104
if (!self.timestampValue || !self.ipValue || self.ipValue.length == 0) {
95105
return NO;
96106
}
97107
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
98-
return currentTimestamp > self.timestampValue.doubleValue + self.ttlValue.doubleValue;
108+
return currentTimestamp < (self.timestampValue.doubleValue + kQNGlobalConfiguration.dnsCacheMaxTTL);
99109
}
100110

101111
- (NSString *)toJsonInfo{
@@ -298,7 +308,12 @@ - (void)checkWhetherCachedDnsValid{
298308
@synchronized (self) {
299309
addressList = self.addressDictionary[host];
300310
}
301-
return addressList;
311+
312+
if (addressList && addressList.count > 0 && [addressList.firstObject isValid]) {
313+
return addressList;
314+
} else {
315+
return nil;
316+
}
302317
}
303318

304319
//MARK: --
@@ -376,7 +391,7 @@ - (BOOL)preFetchHost:(NSString *)preHost
376391
}
377392

378393
NSArray<QNDnsNetworkAddress *>* preAddressList = self.addressDictionary[preHost];
379-
if (preAddressList && [preAddressList.firstObject isValid]) {
394+
if (preAddressList && ![preAddressList.firstObject needRefresh]) {
380395
return YES;
381396
}
382397

@@ -657,6 +672,7 @@ - (void)addDnsLocalLoadTransaction{
657672
[kQNDnsPrefetch localFetch];
658673
}];
659674
[[QNTransactionManager shared] addTransaction:transaction];
675+
[self setDnsCheckWhetherCachedValidTransactionAction];
660676
});
661677
}
662678

0 commit comments

Comments
 (0)