【问题标题】:iOS Video Caching - Manual cache deletioniOS 视频缓存 - 手动缓存删除
【发布时间】:2020-08-11 14:12:30
【问题描述】:

我有一个使用 React Native Video with iOS caching 的 React Native 应用程序。我一直在研究RCTVideoCache.m 中的一种方法,该方法将手动删除特定缓存键的数据。根据视频库用于缓存的SPTPersistentCache的文档,可以通过locking/unlocking a fileinvoking a wipe删除数据,或者通过removeDataForKeys的方法检查SPTPersistentCache.h的源代码后删除数据。

这两种方法我都试过了,但是都不成功。

我的第一次尝试中,我使用的是wipeLockedFiles。我在RCTVideoCache.m 中创建了一个deleteFromCache() 方法。由于默认情况下我的所有视频文件都处于解锁状态,因此在这种方法中,我试图将与我的cacheKeyinvoke a wipe 对应的文件锁定在所有锁定的文件(仅包含我的目标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)); 都没有改变,我也可以手动验证文件是否没有被删除。

我真的被困住了,不知道我在这两种情况下编写的代码有什么问题,以及为什么它们都不起作用。我将不胜感激!

【问题讨论】:

  • 你能分享整个RCTVideoCache.m,修改版吗?也许作为 GitHub 要点。
  • 这是错误的代码吗? } onQueue:dispatch_get_main_queue()]; - 我在你的第二个例子中看不到它属于什么
  • @ivanmoskalev 我没有修改原始RCTVideoCache.m 中的任何其他内容。我只添加了上面提供的方法——deleteFromCache
  • @ewizard 这是我用来满足removeDataForKeys()参数的一些代码。我看到other parts of RCTVideoCache.m(line 73)中也使用了同样的方法。
  • @ivanmoskalev 我刚刚上传了它:gist.github.com/iAmOptimus/767342f4eba52e5bff0f0027ee5423ad

标签: ios objective-c react-native caching


【解决方案1】:

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

【讨论】:

  • 谢谢你,这有效。我在stringWithFormat:@"%@/%@"中添加了/,文件删除成功!
  • @IamOptimus 太棒了!谢谢!
【解决方案2】:

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

试试这样的:

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

【讨论】:

  • 我刚试过这个。在终端内部,我可以看到删除后缓存大小正确更新,并显示操作已成功完成的响应消息。但是,文件仍然被缓存。 React Native Video 仍然会在缓存中定位文件并播放它而无需下载。使用Filza,我可以验证视频仍在应用程序的tmp/rct.video.cache 文件夹中。你知道为什么这种方法不会删除视频数据文件吗?
猜你喜欢
  • 1970-01-01
  • 2012-06-05
  • 2020-07-24
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
  • 1970-01-01
  • 2016-01-23
  • 2018-03-01
相关资源
最近更新 更多