补充调整后的缓存方案
This commit is contained in:
@@ -121,8 +121,6 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
|
||||
@property(nonatomic,assign) BOOL isReloadTicket;
|
||||
///房间外的飘屏
|
||||
@property(nonatomic,strong) PIFullScreenBannerAnimation *roomAnimation;
|
||||
///缓存礼物的次数,超过了就不缓存了
|
||||
@property(nonatomic,assign) int downloadGiftCount;
|
||||
|
||||
@end
|
||||
|
||||
@@ -326,55 +324,42 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
|
||||
}
|
||||
///缓存礼物特效
|
||||
-(void)requestGiftList{
|
||||
[self removeOldVersionCacheFromOldMethod];
|
||||
|
||||
@kWeakify(self);
|
||||
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
||||
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
||||
AFNetworkReachabilityManager *networkManager = [AFNetworkReachabilityManager sharedManager];
|
||||
[networkManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
||||
@kStrongify(self);
|
||||
switch (status) {
|
||||
case AFNetworkReachabilityStatusUnknown:
|
||||
break;
|
||||
case AFNetworkReachabilityStatusNotReachable:
|
||||
|
||||
break;
|
||||
case AFNetworkReachabilityStatusReachableViaWWAN:
|
||||
case AFNetworkReachabilityStatusReachableViaWiFi:
|
||||
{
|
||||
if(self.isFirstReachability == NO){
|
||||
|
||||
[self dealWithGiftList];
|
||||
[Api requestCacheGiftDynamicEffectList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
|
||||
if(code == 200){
|
||||
self.isFirstReachability = YES;
|
||||
NSMutableArray *giftList = [[NSMutableArray alloc]initWithArray:data.data ?: @[]];
|
||||
NSArray *elfList = @[@"https://image.hfighting.com/fenjie2.mp4",@"https://image.hfighting.com/treasure_fairy_ten.mp4",@"https://image.hfighting.com/hecheng3.mp4",@"https://image.hfighting.com/treasure_fairy_one.mp4"];
|
||||
[giftList addObjectsFromArray:elfList];
|
||||
NSSet *setList = [[NSSet alloc]initWithArray:giftList];///去重,防止有重复的
|
||||
[self cacheGiftDynamicEffectList:setList];
|
||||
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
|
||||
if(self.isFirstReachability == NO){
|
||||
[Api requestCacheGiftDynamicEffectList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
|
||||
if(code == 200){
|
||||
self.isFirstReachability = YES;
|
||||
NSArray *allResourceList = data.data;
|
||||
NSSet *allResourceSet = [[NSSet alloc] initWithArray:allResourceList];
|
||||
NSMutableArray *allResourceURLs = @[@"https://image.hfighting.com/fenjie2.mp4",
|
||||
@"https://image.hfighting.com/treasure_fairy_ten.mp4",
|
||||
@"https://image.hfighting.com/hecheng3.mp4",
|
||||
@"https://image.hfighting.com/treasure_fairy_one.mp4"].mutableCopy;
|
||||
for (NSString *url in allResourceSet) {
|
||||
NSString *encodingUrl = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
|
||||
[allResourceURLs addObject:encodingUrl];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
[[UploadFile share] startBatchDownloadWithURLs:allResourceURLs];
|
||||
}
|
||||
}];
|
||||
} else {
|
||||
[[UploadFile share] resumeBatchDownload];
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
} else {
|
||||
[[UploadFile share] pauseBatchDownload];
|
||||
}
|
||||
}];
|
||||
|
||||
|
||||
[networkManager startMonitoring];
|
||||
}
|
||||
|
||||
|
||||
///缓存新的礼物动效数据
|
||||
-(void)cacheGiftDynamicEffectList:(NSSet *)list{
|
||||
[UploadFile downloadGiftDynamicEffectWithList:list completion:^(BOOL isSuccess, NSMutableArray * _Nonnull failList) {
|
||||
|
||||
}];
|
||||
}
|
||||
|
||||
-(void)dealWithGiftList{
|
||||
///删除旧数据缓存,
|
||||
-(void)removeOldVersionCacheFromOldMethod{
|
||||
//MARK: 删除旧数据缓存,预计 2 个版本后可以删除此方法
|
||||
NSString *svgaFileName = @"/GiftSvga";
|
||||
NSString *svgaFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:svgaFileName];
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:svgaFilePath]) {
|
||||
@@ -382,7 +367,6 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
|
||||
if(isSuccess == YES){
|
||||
NSLog(@"删除成功");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
NSString *mp4FileName = @"/GiftMp4";
|
||||
@@ -392,7 +376,6 @@ UIKIT_EXTERN NSString *kTabShowAnchorCardKey;
|
||||
if(isSuccess == YES){
|
||||
NSLog(@"删除成功");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -37,9 +37,17 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
failure:(void (^)(NSNumber *resCode, NSString *message))failure;
|
||||
|
||||
///下载资料
|
||||
-(void)downloadAnimationFileName:(NSString *)fileName localPath:(NSString *)localPath completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion;
|
||||
- (void)downloadAnimationFileName:(NSString *)fileName localPath:(NSString *)localPath completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion;
|
||||
|
||||
///下载礼物特效
|
||||
+(void)downloadGiftDynamicEffectWithList:(NSSet *)list completion:(void (^) (BOOL isSuccess, NSMutableArray *editAudioPath))completion;
|
||||
|
||||
|
||||
// 批量下载
|
||||
- (void)startBatchDownloadWithURLs:(NSArray<NSString *> *)URLs;
|
||||
- (void)pauseBatchDownload;
|
||||
- (void)resumeBatchDownload;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
@@ -15,6 +15,11 @@ static UploadFile* manager;
|
||||
// 一个脚手架实例
|
||||
@property (nonatomic) QCloudCredentailFenceQueue* credentialFenceQueue;
|
||||
@property(nonatomic,strong) UploadFileModel *fileModel;
|
||||
|
||||
// MARK: 批量下载部分, 后续要新建一个 object 来承载业务
|
||||
@property (nonatomic, strong) AFHTTPSessionManager *manager;
|
||||
@property (nonatomic, strong) NSMutableArray<NSURLSessionDownloadTask *> *tasks;
|
||||
|
||||
@end
|
||||
|
||||
@implementation UploadFile
|
||||
@@ -258,9 +263,12 @@ static UploadFile* manager;
|
||||
[[QCloudCOSTransferMangerService defaultCOSTransferManager] DownloadObject:request];
|
||||
|
||||
}
|
||||
+(void)downloadAudioWithFileName:(NSString *)fileName musicUrl:(NSString *)musicUrl mainFileName:(NSString *)mainFileName completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion {
|
||||
+(void)downloadAudioWithFileName:(NSString *)fileName
|
||||
musicUrl:(NSString *)musicUrl
|
||||
mainFileName:(NSString *)mainFileName
|
||||
completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion {
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
NSURL *url = [NSURL URLWithString:[musicUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
|
||||
NSURL *url = [NSURL URLWithString:[musicUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL :url];
|
||||
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
|
||||
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
|
||||
@@ -276,49 +284,91 @@ static UploadFile* manager;
|
||||
[download resume];
|
||||
}
|
||||
|
||||
+(void)downloadGiftDynamicEffectWithList:(NSSet *)list completion:(void (^) (BOOL isSuccess, NSMutableArray *editAudioPath))completion{
|
||||
// 批量下载
|
||||
- (void)startBatchDownloadWithURLs:(NSArray<NSString *> *)URLs {
|
||||
if (_manager == nil) {
|
||||
_manager = [AFHTTPSessionManager manager];
|
||||
_manager.operationQueue.maxConcurrentOperationCount = 10;
|
||||
_tasks = [NSMutableArray array];
|
||||
}
|
||||
|
||||
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
|
||||
manager.operationQueue.maxConcurrentOperationCount = 10; // 控制并发数量
|
||||
dispatch_group_t requestGroup = dispatch_group_create();
|
||||
NSMutableArray *failList = [NSMutableArray array];
|
||||
for (NSString *giftUrl in list) {
|
||||
NSString *encodingUrl = [giftUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
|
||||
NSString *fileName = [[encodingUrl componentsSeparatedByString:@"/"] lastObject];
|
||||
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:@"GiftDynamicEffectList"];
|
||||
NSString *fullPath = [filePath stringByAppendingPathComponent:fileName];
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
|
||||
if (URLs.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
[self createCacheLocalPath];
|
||||
|
||||
for (NSString *urlStr in URLs) {
|
||||
// 缓存本地目标位置
|
||||
NSURL *destinationURL = [self localPath:urlStr];
|
||||
|
||||
// 检查文件是否已经存在
|
||||
if ([[NSFileManager defaultManager] fileExistsAtPath:[destinationURL path]]) {
|
||||
continue;
|
||||
}
|
||||
NSFileManager *fileMgr = [[NSFileManager alloc] init];
|
||||
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
|
||||
dispatch_group_enter(requestGroup);
|
||||
NSURL *url = [NSURL URLWithString:[encodingUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL :url];
|
||||
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
|
||||
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
|
||||
return [NSURL fileURLWithPath:fullPath];
|
||||
|
||||
NSURL *url = [NSURL URLWithString:urlStr];
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:url];
|
||||
|
||||
@kWeakify(self);
|
||||
__block NSURLSessionDownloadTask *downloadTask;
|
||||
downloadTask = [self.manager downloadTaskWithRequest:request
|
||||
progress:nil
|
||||
destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
|
||||
return destinationURL;
|
||||
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
|
||||
dispatch_group_leave(requestGroup);
|
||||
if (!error) {
|
||||
NSLog(@"下载完成");
|
||||
} else {
|
||||
NSLog(@"下载失败");
|
||||
[failList addObject:encodingUrl];
|
||||
@kStrongify(self);
|
||||
if ([self.tasks containsObject:downloadTask]) {
|
||||
[self.tasks removeObject:downloadTask];
|
||||
}
|
||||
}];
|
||||
[download resume];
|
||||
}
|
||||
dispatch_group_notify(requestGroup, dispatch_get_main_queue(), ^{
|
||||
if(failList.count > 0){
|
||||
completion(NO,failList);
|
||||
NSLog(@"下载有失败,失败数:%ld,成功数:%ld,总数:%ld",failList.count,list.count - failList.count,list.count);
|
||||
}else{
|
||||
NSLog(@"下载全部完成,总数:%ld",list.count);
|
||||
completion(NO,nil);
|
||||
}
|
||||
});
|
||||
|
||||
[self.tasks addObject:downloadTask];
|
||||
}
|
||||
|
||||
[self resumeBatchDownload];
|
||||
}
|
||||
|
||||
- (void)pauseBatchDownload {
|
||||
for (NSURLSessionDownloadTask *task in self.tasks) {
|
||||
if (task.state == NSURLSessionTaskStateRunning) {
|
||||
[task suspend];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)resumeBatchDownload {
|
||||
for (NSURLSessionDownloadTask *task in self.tasks) {
|
||||
if (task.state == NSURLSessionTaskStateSuspended) {
|
||||
[task resume];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (void)createCacheLocalPath {
|
||||
NSError *error = nil;
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
NSString *filePath = [self cacheFilePath];
|
||||
if ([fileManager fileExistsAtPath:filePath] == NO) {
|
||||
NSFileManager *fileMgr = [[NSFileManager alloc] init];
|
||||
BOOL success = [fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
|
||||
if (success) {
|
||||
NSLog(@"Folder created successfully.");
|
||||
} else {
|
||||
NSLog(@"Could not create folder: %@", [error localizedDescription]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
- (NSURL *)localPath:(NSString *)encodingUrl {
|
||||
NSString *fileName = [[encodingUrl componentsSeparatedByString:@"/"] lastObject];
|
||||
NSString *filePath = [self cacheFilePath];
|
||||
NSString *cacheLocalPath = [filePath stringByAppendingPathComponent:fileName];
|
||||
return [NSURL fileURLWithPath:cacheLocalPath];
|
||||
}
|
||||
|
||||
- (NSString *)cacheFilePath {
|
||||
return [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:@"GiftDynamicEffectList"];
|
||||
}
|
||||
|
||||
@end
|
||||
|
Reference in New Issue
Block a user