iOS视频缓存-手动删除缓存



我有一个React Native应用程序,它使用带有iOS缓存的React NativeVideo。我一直在RCTVideoCache.m中研究一种方法,该方法可以手动删除特定缓存键的数据。根据视频库用于缓存的SPTPersistentCache的文档,可以通过锁定/解锁文件并调用擦除来删除数据,也可以通过名为removeDataForKeys的方法检查SPTPersistentCache.h的源代码来删除数据。

然而,我尝试了两种方法,都没有成功。

我的第一次尝试中,我使用的是wipeLockedFiles。我在RCTVideoCache.m中创建了一个deleteFromCache()方法。由于我的所有视频文件在默认情况下都是解锁的,因此在该方法中,我试图锁定与我的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中创建了一个deleteFromCache()方法,它看起来像这样:

- (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);
}

在第二种方式中,没有错误,但是,密钥的数据永远不会被删除。此外,两个NSLogs用于在端子内部输出null的响应。

我100%确信我提供给我的deleteFromCache()方法的cacheKey是正确的,并且存在与之对应的数据。然而,在这两种方法中,NSLog(@"Size = %@", @(self.videoCache.totalUsedSizeInBytes));都不会更改,我也可以手动验证文件是否已删除。

我真的被卡住了,不知道我在这两种情况下写的代码出了什么问题,也不知道为什么它们都不起作用。如果能在这方面得到任何帮助,我将不胜感激!

您可以删除所有子文件夹的文件(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];
}
}

我运行了您的示例,发现您使用的方法签名不正确。这些方法根本不存在于缓存库中,它们的签名是不同的。

试试这样的东西:

- (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));肯定是在实际删除发生之前调用的。在我上面发布的示例中,我已经将日志记录语句移动到回调闭包中,以便它报告删除前后的大小。

最新更新