Skip to content

Commit e152c33

Browse files
committed
Fixed script & download manager batch request may block main queue
1 parent 9b5a011 commit e152c33

File tree

2 files changed

+124
-111
lines changed
  • VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager
  • VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/Common/DownLoad

2 files changed

+124
-111
lines changed

VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VideoPlsLuaViewManagerSDK/VPLua/Manager/VPLuaScriptManager.m

Lines changed: 68 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "VPUPRSAUtil.h"
1717
#import "VPUPBase64Util.h"
1818
#import "VPLuaSDK.h"
19+
1920
#import "VPUPAESUtil.h"
2021
#import "VPUPPathUtil.h"
2122
#import "VPUPMD5Util.h"
@@ -32,6 +33,7 @@ @interface VPLuaScriptManager ()
3233
@property (nonatomic, copy) NSString *tempVersionFilePath;
3334
@property (nonatomic, weak) id<VPUPHTTPAPIManager>apiManager;
3435
@property (nonatomic, strong) VPUPPrefetchManager *prefetchManager;
36+
@property (nonatomic, strong) dispatch_queue_t luaScriptQueue;
3537

3638
@end
3739

@@ -48,6 +50,7 @@ - (instancetype)initWithLuaStorePath:(NSString *)path
4850
_versionFilePath = [path stringByAppendingPathComponent:@"version.json"];
4951
_nativeVersion = nativeVersion;
5052
_apiManager = apiManager;
53+
_luaScriptQueue = dispatch_queue_create("com.videopls.lua.scriptManager", DISPATCH_QUEUE_SERIAL);
5154
[self getLuaVersionInfoWithVersionUrl:url];
5255
}
5356

@@ -119,7 +122,7 @@ - (void)downloadWithFileUrl:(NSDictionary *)data {
119122
[weakSelf error:error type:VPLuaScriptManagerErrorTypeDownloadFile];
120123
return;
121124
}
122-
// [weakSelf unzipWithFilePath:filePath.relativePath data:data];
125+
// [weakSelf unzipWithFilePath:filePath.relativePath data:data];
123126
NSString *fileMD5 = [VPUPMD5Util md5File:[filePath path] size:0];
124127
if ([fileMD5 isEqualToString:[data objectForKey:@"fileMd5"]]) {
125128
[weakSelf checkFilesChange];
@@ -133,37 +136,40 @@ - (void)downloadWithFileUrl:(NSDictionary *)data {
133136
}
134137

135138
- (void)checkFilesChange {
136-
NSString *localFilesPath = [self.luaPath stringByAppendingPathComponent:@"manifest.json"];
137-
NSString *localFilesString = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:localFilesPath] encoding:NSUTF8StringEncoding error:nil];
138-
// NSDictionary *localFiles = VPUP_JsonToDictionary(localFilesString);
139-
// NSArray *localFilesList = [localFiles objectForKey:@"data"];
140-
NSArray *localFilesList = (NSArray *)VPUP_JsonToDictionary(localFilesString);
141-
142-
NSString *downloadFilesPath = [self.tempVersionFilePath stringByAppendingPathComponent:@"manifest.json"];
143-
NSString *downloadFilesString = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:downloadFilesPath] encoding:NSUTF8StringEncoding error:nil];
144-
NSArray *downloadFilesList = (NSArray *)VPUP_JsonToDictionary(downloadFilesString);
145-
// NSDictionary *downloadFiles = VPUP_JsonToDictionary(downloadFilesString);
146-
// NSArray *downloadFilesList = [downloadFiles objectForKey:@"data"];
147-
148-
NSMutableArray *needDownloadFilesList = [NSMutableArray arrayWithCapacity:0];
149-
for (NSDictionary *downloadFile in downloadFilesList) {
150-
BOOL needDownload = YES;
151-
for (NSDictionary *localFile in localFilesList) {
152-
if ([[downloadFile objectForKey:@"name"] isEqualToString:[localFile objectForKey:@"name"]] && [[downloadFile objectForKey:@"md5"] isEqualToString:[localFile objectForKey:@"md5"]]) {
153-
needDownload = NO;
154-
break;
139+
dispatch_async(_luaScriptQueue, ^{
140+
141+
NSString *localFilesPath = [self.luaPath stringByAppendingPathComponent:@"manifest.json"];
142+
NSString *localFilesString = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:localFilesPath] encoding:NSUTF8StringEncoding error:nil];
143+
// NSDictionary *localFiles = VPUP_JsonToDictionary(localFilesString);
144+
// NSArray *localFilesList = [localFiles objectForKey:@"data"];
145+
NSArray *localFilesList = (NSArray *)VPUP_JsonToDictionary(localFilesString);
146+
147+
NSString *downloadFilesPath = [self.tempVersionFilePath stringByAppendingPathComponent:@"manifest.json"];
148+
NSString *downloadFilesString = [NSString stringWithContentsOfURL:[NSURL fileURLWithPath:downloadFilesPath] encoding:NSUTF8StringEncoding error:nil];
149+
NSArray *downloadFilesList = (NSArray *)VPUP_JsonToDictionary(downloadFilesString);
150+
// NSDictionary *downloadFiles = VPUP_JsonToDictionary(downloadFilesString);
151+
// NSArray *downloadFilesList = [downloadFiles objectForKey:@"data"];
152+
153+
NSMutableArray *needDownloadFilesList = [NSMutableArray arrayWithCapacity:0];
154+
for (NSDictionary *downloadFile in downloadFilesList) {
155+
BOOL needDownload = YES;
156+
for (NSDictionary *localFile in localFilesList) {
157+
if ([[downloadFile objectForKey:@"name"] isEqualToString:[localFile objectForKey:@"name"]] && [[downloadFile objectForKey:@"md5"] isEqualToString:[localFile objectForKey:@"md5"]]) {
158+
needDownload = NO;
159+
break;
160+
}
161+
}
162+
if (needDownload) {
163+
[needDownloadFilesList addObject:downloadFile];
155164
}
156165
}
157-
if (needDownload) {
158-
[needDownloadFilesList addObject:downloadFile];
166+
if (needDownloadFilesList.count > 0) {
167+
[self downloadFilesList:needDownloadFilesList];
159168
}
160-
}
161-
if (needDownloadFilesList.count > 0) {
162-
[self downloadFilesList:needDownloadFilesList];
163-
}
164-
else {
165-
[self downloadSuccess:YES];
166-
}
169+
else {
170+
[self downloadSuccess:YES];
171+
}
172+
});
167173
}
168174

169175
- (void)downloadFilesList:(NSArray *)filesList {
@@ -203,38 +209,40 @@ - (void)downloadFilesList:(NSArray *)filesList {
203209
}
204210

205211
- (void)checkDownLoadFiles:(NSArray *)filesList {
206-
if (!filesList || filesList.count == 0) {
207-
[self downloadSuccess:YES];
208-
return;
209-
}
210-
211-
NSError *error = nil;
212-
for (NSDictionary *file in filesList) {
213-
NSString *filePath = [self.tempVersionFilePath stringByAppendingPathComponent:[file objectForKey:@"name"]];
214-
NSString *fileMD5 = [VPUPMD5Util md5File:filePath size:0];
215-
if (![fileMD5 isEqualToString:[file objectForKey:@"md5"]]) {
216-
error = [NSError errorWithDomain:VPLuaScriptManagerErrorDomain code:-3003 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"filePath:%@, file:%@, download file md5 error", filePath, file]}];
217-
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
218-
}
219-
}
220-
221-
if (error) {
222-
[self error:error type:VPLuaScriptManagerErrorTypFileMD5];
223-
}
224-
else {
225-
[self copyFileFromPath:self.tempVersionFilePath toPath:self.luaPath];
226-
// 将版本写入文件,若失败删除
227-
NSError *writeError = nil;
228-
[self.versionData writeToFile:self.versionFilePath atomically:YES encoding:NSUTF8StringEncoding error:&writeError];
229-
if (writeError) {
230-
[self removeAllFileAtLuaPath];
231-
[self error:writeError type:VPLuaScriptManagerErrorTypeWriteVersionFile];
212+
dispatch_async(_luaScriptQueue, ^{
213+
if (!filesList || filesList.count == 0) {
214+
[self downloadSuccess:YES];
232215
return;
233216
}
234-
// 最终完成
235-
[self downloadSuccess:YES];
236-
[[NSFileManager defaultManager] removeItemAtPath:self.tempVersionFilePath error:nil];
237-
}
217+
218+
NSError *error = nil;
219+
for (NSDictionary *file in filesList) {
220+
NSString *filePath = [self.tempVersionFilePath stringByAppendingPathComponent:[file objectForKey:@"name"]];
221+
NSString *fileMD5 = [VPUPMD5Util md5File:filePath size:0];
222+
if (![fileMD5 isEqualToString:[file objectForKey:@"md5"]]) {
223+
error = [NSError errorWithDomain:VPLuaScriptManagerErrorDomain code:-3003 userInfo:@{NSLocalizedDescriptionKey:[NSString stringWithFormat:@"filePath:%@, file:%@, download file md5 error", filePath, file]}];
224+
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
225+
}
226+
}
227+
228+
if (error) {
229+
[self error:error type:VPLuaScriptManagerErrorTypFileMD5];
230+
}
231+
else {
232+
[self copyFileFromPath:self.tempVersionFilePath toPath:self.luaPath];
233+
// 将版本写入文件,若失败删除
234+
NSError *writeError = nil;
235+
[self.versionData writeToFile:self.versionFilePath atomically:YES encoding:NSUTF8StringEncoding error:&writeError];
236+
if (writeError) {
237+
[self removeAllFileAtLuaPath];
238+
[self error:writeError type:VPLuaScriptManagerErrorTypeWriteVersionFile];
239+
return;
240+
}
241+
// 最终完成
242+
[self downloadSuccess:YES];
243+
[[NSFileManager defaultManager] removeItemAtPath:self.tempVersionFilePath error:nil];
244+
}
245+
});
238246
}
239247

240248
- (void)unzipWithFilePath:(NSString *)path data:(NSDictionary *)data {
@@ -245,7 +253,7 @@ - (void)unzipWithFilePath:(NSString *)path data:(NSDictionary *)data {
245253
NSFileManager *fileManager = [[NSFileManager alloc] init];
246254
NSArray* array = [fileManager contentsOfDirectoryAtPath:luaZipFilesPath error:nil];
247255
for(int i = 0; i<[array count]; i++) {
248-
256+
249257
NSString *fullPath = [luaZipFilesPath stringByAppendingPathComponent:[array objectAtIndex:i]];
250258
if ([[fullPath lastPathComponent] containsString:@"zip"]) {
251259
LVZipArchive *archive = [LVZipArchive archiveWithData:[NSData dataWithContentsOfFile:fullPath]];

VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/VideoPlsUtilsPlatformSDK/Common/DownLoad/VPUPDownloaderManager.m

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ @interface VPUPDownloaderManager()<VPUPResumeDownloaderDelegate>
3838
@property (nonatomic, strong) NSMutableArray *requestArray;//等待下载的request
3939
@property (nonatomic, strong) NSMutableArray *downloadingRequestArray;//正在下载的request
4040
@property (readwrite, nonatomic, strong) NSLock *lock;
41+
@property (nonatomic) dispatch_queue_t downloadBatchQueue;
4142

4243
@end
4344

@@ -63,6 +64,7 @@ - (instancetype)init
6364
self.downloadingRequestArray = [NSMutableArray arrayWithCapacity:0];
6465
self.lock = [[NSLock alloc] init];
6566
self.lock.name = VPUPDownloaderManagerLockName;
67+
self.downloadBatchQueue = dispatch_queue_create("com.videopls.download.batch", DISPATCH_QUEUE_SERIAL);
6668
}
6769
return self;
6870
}
@@ -96,20 +98,23 @@ - (void)downloadWithRequest:(VPUPDownloadRequest *)request
9698

9799
- (void)downloadWithBatchRequest:(VPUPDownloadBatchRequest *)requests
98100
{
99-
NSParameterAssert(requests);
100-
dispatch_group_t batch_group = dispatch_group_create();
101-
for (VPUPDownloadRequest *request in requests.requestArray)
102-
{
103-
request.completionGroup = batch_group;
104-
dispatch_group_enter(batch_group);
105-
[self downloadWithRequest:request];
106-
}
107-
108-
dispatch_queue_t callbackQueue = [requests callbackQueue] ? : dispatch_get_main_queue();
109-
dispatch_group_notify(batch_group, callbackQueue, ^{
110-
if (requests.completionHandler) {
111-
requests.completionHandler(requests);
101+
dispatch_async(self.downloadBatchQueue, ^(void) {
102+
103+
NSParameterAssert(requests);
104+
dispatch_group_t batch_group = dispatch_group_create();
105+
for (VPUPDownloadRequest *request in requests.requestArray)
106+
{
107+
request.completionGroup = batch_group;
108+
dispatch_group_enter(batch_group);
109+
[self downloadWithRequest:request];
112110
}
111+
112+
dispatch_queue_t callbackQueue = [requests callbackQueue] ? : dispatch_get_main_queue();
113+
dispatch_group_notify(batch_group, callbackQueue, ^{
114+
if (requests.completionHandler) {
115+
requests.completionHandler(requests);
116+
}
117+
});
113118
});
114119
}
115120

@@ -125,7 +130,7 @@ - (void)createDownloader
125130
{
126131
downloader.isForceDownload = YES;
127132
}
128-
133+
129134
downloader.delegate = self;
130135
request.state = VPUPDownloadRequestStateLoading;
131136
[self.downloaderArray addObject:downloader];
@@ -165,52 +170,52 @@ - (void)downloadCancelRequest:(VPUPDownloadRequest *)request
165170
userInfo:userInfo];
166171

167172
switch (request.state) {
168-
case VPUPDownloadRequestStateWait:
169-
[self.lock lock];
170-
if ([self.requestArray containsObject:request])
173+
case VPUPDownloadRequestStateWait:
174+
[self.lock lock];
175+
if ([self.requestArray containsObject:request])
176+
{
177+
[self.requestArray removeObject:request];
178+
request.state = VPUPDownloadRequestStateError;
179+
180+
[self callRequestCompletion:request fileURL:nil error:error];
181+
}
182+
[self.lock unlock];
183+
break;
184+
case VPUPDownloadRequestStateLoading:
185+
[self.lock lock];
186+
if ([self.downloadingRequestArray containsObject:request])
187+
{
188+
BOOL isNeedCancelDownloader = YES;
189+
for (VPUPDownloadRequest *tempRequest in self.downloadingRequestArray)
171190
{
172-
[self.requestArray removeObject:request];
173-
request.state = VPUPDownloadRequestStateError;
174-
175-
[self callRequestCompletion:request fileURL:nil error:error];
191+
if (tempRequest != request && [tempRequest.downloadUrl isEqualToString:request.downloadUrl])
192+
{
193+
isNeedCancelDownloader = NO;
194+
break;
195+
}
176196
}
177-
[self.lock unlock];
178-
break;
179-
case VPUPDownloadRequestStateLoading:
180-
[self.lock lock];
181-
if ([self.downloadingRequestArray containsObject:request])
197+
[self.downloadingRequestArray removeObject:request];
198+
if (isNeedCancelDownloader)
182199
{
183-
BOOL isNeedCancelDownloader = YES;
184-
for (VPUPDownloadRequest *tempRequest in self.downloadingRequestArray)
200+
VPUPResumeDownloader *downloader = nil;
201+
for (VPUPResumeDownloader *tempDownloader in self.downloaderArray)
185202
{
186-
if (tempRequest != request && [tempRequest.downloadUrl isEqualToString:request.downloadUrl])
203+
if ([tempDownloader.downloadUrl isEqualToString:request.downloadUrl])
187204
{
188-
isNeedCancelDownloader = NO;
205+
downloader = tempDownloader;
189206
break;
190207
}
191208
}
192-
[self.downloadingRequestArray removeObject:request];
193-
if (isNeedCancelDownloader)
194-
{
195-
VPUPResumeDownloader *downloader = nil;
196-
for (VPUPResumeDownloader *tempDownloader in self.downloaderArray)
197-
{
198-
if ([tempDownloader.downloadUrl isEqualToString:request.downloadUrl])
199-
{
200-
downloader = tempDownloader;
201-
break;
202-
}
203-
}
204-
[downloader cancel];
205-
[downloader invalidate];
206-
[self.downloaderArray removeObject:downloader];
207-
}
208-
request.state = VPUPDownloadRequestStateError;
209-
210-
[self callRequestCompletion:request fileURL:nil error:error];
209+
[downloader cancel];
210+
[downloader invalidate];
211+
[self.downloaderArray removeObject:downloader];
211212
}
212-
[self.lock unlock];
213-
break;
213+
request.state = VPUPDownloadRequestStateError;
214+
215+
[self callRequestCompletion:request fileURL:nil error:error];
216+
}
217+
[self.lock unlock];
218+
break;
214219

215220
default:
216221
break;

0 commit comments

Comments
 (0)