Skip to content

Commit 5d83331

Browse files
committed
Merge pull request #899 from ParsePlatform/nlutsenko.fileCache
Add ability to clear PFFile cache.
2 parents dd531aa + 77e2bee commit 5d83331

File tree

6 files changed

+110
-4
lines changed

6 files changed

+110
-4
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
///--------------------------------------

Tests/Unit/FileControllerTests.m

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,33 @@ - (void)testUploadCancel {
497497
[self waitForTestExpectations];
498498
}
499499

500-
- (void)testClearCaches {
500+
- (void)testClearFileCache {
501+
id mockedDataSource = PFStrictProtocolMock(@protocol(PFFileManagerProvider));
502+
id mockedFileManager = PFStrictClassMock([PFFileManager class]);
503+
504+
NSString *temporaryPath = [self temporaryDirectory];
505+
NSString *downloadsPath = [temporaryPath stringByAppendingPathComponent:@"downloads"];
506+
507+
OCMStub([mockedDataSource fileManager]).andReturn(mockedFileManager);
508+
OCMStub([mockedFileManager parseLocalSandboxDataDirectoryPath]).andReturn(temporaryPath);
509+
OCMStub([mockedFileManager parseCacheItemPathForPathComponent:@"PFFileCache"]).andReturn(downloadsPath);
510+
511+
PFFileController *fileController = [PFFileController controllerWithDataSource:mockedDataSource];
512+
PFFileState *fileState = [[PFMutableFileState alloc] initWithName:@"sampleData"
513+
urlString:temporaryPath
514+
mimeType:@"application/octet-stream"];
515+
516+
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
517+
[[fileController clearFileCacheAsyncForFileWithState:fileState] continueWithBlock:^id(BFTask *task) {
518+
XCTAssertNil(task.error);
519+
[expectation fulfill];
520+
return nil;
521+
}];
522+
523+
[self waitForTestExpectations];
524+
}
525+
526+
- (void)testClearAllFilesCache {
501527
id mockedDataSource = PFStrictProtocolMock(@protocol(PFFileManagerProvider));
502528
id mockedFileManager = PFStrictClassMock([PFFileManager class]);
503529

@@ -511,7 +537,7 @@ - (void)testClearCaches {
511537
PFFileController *fileController = [PFFileController controllerWithDataSource:mockedDataSource];
512538

513539
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
514-
[[fileController clearFileCacheAsync] continueWithBlock:^id(BFTask *task) {
540+
[[fileController clearAllFileCacheAsync] continueWithBlock:^id(BFTask *task) {
515541
XCTAssertNil(task.error);
516542
[expectation fulfill];
517543
return nil;

Tests/Unit/FileUnitTests.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,4 +508,40 @@ - (void)testCancel {
508508
[self waitForTestExpectations];
509509
}
510510

511+
- (void)testClearCachedData {
512+
id mockedFileController = [Parse _currentManager].coreManager.fileController;
513+
514+
PFFile *file = [PFFile fileWithName:@"a" data:[NSData data]];
515+
OCMExpect([mockedFileController clearFileCacheAsyncForFileWithState:file.state]).andReturn([BFTask taskWithResult:nil]);
516+
517+
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
518+
[[file clearCachedDataInBackground] continueWithBlock:^id _Nullable(BFTask * _Nonnull task) {
519+
XCTAssertNil(task.result);
520+
XCTAssertFalse(task.faulted);
521+
XCTAssertFalse(task.cancelled);
522+
[expectation fulfill];
523+
return nil;
524+
}];
525+
[self waitForTestExpectations];
526+
527+
OCMVerifyAll(mockedFileController);
528+
}
529+
530+
- (void)testClearAllCachedData {
531+
id mockedFileController = [Parse _currentManager].coreManager.fileController;
532+
OCMExpect([mockedFileController clearAllFileCacheAsync]).andReturn([BFTask taskWithResult:nil]);
533+
534+
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
535+
[[PFFile clearAllCachedDataInBackground] continueWithBlock:^id _Nullable(BFTask * _Nonnull task) {
536+
XCTAssertNil(task.result);
537+
XCTAssertFalse(task.faulted);
538+
XCTAssertFalse(task.cancelled);
539+
[expectation fulfill];
540+
return nil;
541+
}];
542+
[self waitForTestExpectations];
543+
544+
OCMVerifyAll(mockedFileController);
545+
}
546+
511547
@end

0 commit comments

Comments
 (0)