【发布时间】:2020-08-11 14:12:30
【问题描述】:
我有一个使用 React Native Video with iOS caching 的 React Native 应用程序。我一直在研究RCTVideoCache.m 中的一种方法,该方法将手动删除特定缓存键的数据。根据视频库用于缓存的SPTPersistentCache的文档,可以通过locking/unlocking a file和invoking a wipe删除数据,或者通过removeDataForKeys的方法检查SPTPersistentCache.h的源代码后删除数据。
这两种方法我都试过了,但是都不成功。
在我的第一次尝试中,我使用的是wipeLockedFiles。我在RCTVideoCache.m 中创建了一个deleteFromCache() 方法。由于默认情况下我的所有视频文件都处于解锁状态,因此在这种方法中,我试图将与我的cacheKey 和invoke 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(line73)中也使用了同样的方法。 -
@ivanmoskalev 我刚刚上传了它:gist.github.com/iAmOptimus/767342f4eba52e5bff0f0027ee5423ad
标签: ios objective-c react-native caching