Skip to content

Commit 953182c

Browse files
committed
Add ability to clear file cache.
1 parent dd531aa commit 953182c

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

Parse/Internal/File/Controller/PFFileController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@
9494
#pragma mark - Cache
9595
///--------------------------------------
9696

97-
- (BFTask<PFVoid> *)clearFileCacheAsync;
97+
- (BFTask<PFVoid> *)clearFileCacheAsyncForFileWithState:(PFFileState *)fileState;
98+
- (BFTask<PFVoid> *)clearAllFileCacheAsync;
9899

99100
- (NSString *)cachedFilePathForFileState:(PFFileState *)fileState;
100101

Parse/Internal/File/Controller/PFFileController.m

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,18 @@ - (NSString *)cacheFilesDirectoryPath {
259259
return [self.dataSource.fileManager parseCacheItemPathForPathComponent:PFFileControllerCacheDirectoryName_];
260260
}
261261

262-
- (BFTask<PFVoid> *)clearFileCacheAsync {
262+
- (BFTask<PFVoid> *)clearFileCacheAsyncForFileWithState:(PFFileState *)fileState {
263+
return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id{
264+
NSString *filePath = [self cachedFilePathForFileState:fileState];
265+
if (!filePath) {
266+
return nil;
267+
}
268+
// No need to lock on this, since we are removing from a cache directory.
269+
return [PFFileManager removeItemAtPathAsync:filePath withFileLock:NO];
270+
}];
271+
}
272+
273+
- (BFTask<PFVoid> *)clearAllFileCacheAsync {
263274
return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id{
264275
NSString *path = self.cacheFilesDirectoryPath;
265276
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

Parse/PFFile.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,24 @@ NS_ASSUME_NONNULL_BEGIN
366366
*/
367367
- (void)cancel;
368368

369+
///--------------------------------------
370+
#pragma mark - Cache
371+
///--------------------------------------
372+
373+
/**
374+
Clears all cached data for this file.
375+
376+
@return The task, with the result set to `nil` if the operation succeeds.
377+
*/
378+
- (BFTask *)clearCachedDataInBackground;
379+
380+
/**
381+
Clears all cached data for all downloaded files.
382+
383+
@return The task, with the result set to `nil` if the operation succeeds.
384+
*/
385+
+ (BFTask *)clearAllCachedDataInBackground;
386+
369387
@end
370388

371389
NS_ASSUME_NONNULL_END

Parse/PFFile.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ - (void)cancel {
217217
}];
218218
}
219219

220+
#pragma mark Cache
221+
222+
- (BFTask *)clearCachedDataInBackground {
223+
@weakify(self);
224+
return [self.taskQueue enqueue:^id(BFTask *_) {
225+
@strongify(self);
226+
return [[[[self class] fileController] clearFileCacheAsyncForFileWithState:self.state] continueWithSuccessResult:nil];
227+
}];
228+
}
229+
230+
+ (BFTask *)clearAllCachedDataInBackground {
231+
return [[[self fileController] clearAllFileCacheAsync] continueWithSuccessResult:nil];
232+
}
233+
220234
///--------------------------------------
221235
#pragma mark - Private
222236
///--------------------------------------

0 commit comments

Comments
 (0)