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