我有一个使用使用iOS缓存响应本地视频的React本机应用程序。我一直在研究RCTVideoCache.m内部的一种方法,该方法将手动删除特定缓存键的数据。根据视频库用于缓存的SPTPersistentCache文档,数据可以通过锁定/解锁文件和调用擦拭删除,也可以在使用removeDataForKeys方法检查SPTPersistentCache.h源代码之后删除。
然而,我尝试过这两种方法,但都没有成功。
在中,我第一次尝试,我使用的是wipeLockedFiles。我在deleteFromCache()中创建了一个RCTVideoCache.m方法。由于默认情况下我的所有视频文件都是解锁的,所以在这种方法中,我试图在所有锁定的文件上锁定与我的cacheKey和调用擦除对应的文件(该文件将只包含我的目标cacheKey文件),如文档所示。此方法如下所示:
- (void)deleteFromCache:(NSString *)cacheKey withCallback:(void(^)(BOOL))handler;
{
[self.videoCache lockDataForKeys:@[cacheKey] callback:nil queue:nil];
[self.videoCache wipeLockedFiles];
NSLog(@"Size = %@", @(self.videoCache.totalUsedSizeInBytes));
handler(YES);
}以下情况导致编译过程中出现两个错误:
/Users/.../MyApp/node_modules/react-native-video/ios/VideoCaching/RCTVideoCache.m:79:20: error: no visible @interface for 'SPTPersistentCache' declares the selector 'lockDataForKeys:callback:queue:'
[self.videoCache lockDataForKeys:@[cacheKey] callback:nil queue:nil];
~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/.../MyApp/node_modules/react-native-video/ios/VideoCaching/RCTVideoCache.m:80:20: error: no visible @interface for 'SPTPersistentCache' declares the selector 'wipeLockedFiles'
[self.videoCache wipeLockedFiles];
~~~~~~~~~~~~~~~ ^~~~~~~~~~~~~~~我真的不知道为什么在SPTPersistentCache中看不到这些选择器。
在(我的第二次尝试)中,我使用removeDataForKeys()。同样,我在RCTVideoCache.m中创建了一个RCTVideoCache.m方法,如下所示:
- (void)deleteFromCache:(NSString *)cacheKey withCallback:(void(^)(BOOL))handler;
{
[self.videoCache removeDataForKeys:@[cacheKey] callback:^(SPTPersistentCacheResponse * _Nonnull response) {
NSLog(@"Result output: %@", response.output);
NSLog(@"Error output: %@", [response.error localizedDescription]);
} onQueue:dispatch_get_main_queue()];
NSLog(@"Size = %@", @(self.videoCache.totalUsedSizeInBytes));
handler(YES);
}在第二种方式中,没有错误,但是,该键的数据从未被删除。另外,两个NSLog都用于终端内部的响应输出null。
我100%确信我提供给我的deleteFromCache()方法的deleteFromCache()是正确的,并且与它相对应的数据是存在的。但是,在这两种方法中,NSLog(@"Size = %@", @(self.videoCache.totalUsedSizeInBytes));都不会改变,我也可以手动验证文件没有被删除。
我真的被困住了,不知道我在这两种情况下编写的代码有什么问题,也不知道为什么它们都不起作用。我希望能在这方面提供任何帮助!
发布于 2020-05-04 23:12:17
您可以删除所有子文件夹的文件(tmp/rct.video.cache),迭代每个文件:
+ (void)deleteFromCache
{
NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.temporaryCachePath error:NULL];
for (NSString *file in tmpDirectory) {
[[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", self.temporaryCachePath, file] error:NULL];
}
}发布于 2020-05-04 13:20:31
我运行了您的示例,发现您使用了不正确的方法签名。这些方法根本不存在于缓存库中,它们的签名是是不同的。
试着做这样的事情:
- (void)deleteFromCache:(NSString *)cacheKey withCallback:(void(^)(BOOL))handler;
{
NSLog(@"Size before = %@", @(self.videoCache.totalUsedSizeInBytes));
[self.videoCache lockDataForKeys:@[cacheKey] callback:nil onQueue:nil];
[self.videoCache wipeLockedFilesWithCallback:^(SPTPersistentCacheResponse * _Nonnull response) {
NSLog(@"Size after = %@, response = %@", @(self.videoCache.totalUsedSizeInBytes), response);
// Call handler after the files are wiped
handler(YES);
} onQueue:nil];
}我不知道为什么第二种方法不起作用,但在实际删除之前,NSLog(@"Size = %@", @(self.videoCache.totalUsedSizeInBytes));肯定会被调用。在我上面发布的示例中,我将日志语句移动到回调闭包中,以便它报告删除前后的大小。
https://stackoverflow.com/questions/61468773
复制相似问题