Skip to content

Commit a9665d6

Browse files
committed
Merge pull request #719 from ParsePlatform/nlutsenko.file.cache
Fix expensive cached file path getter in FileController.
2 parents 930d96a + d598dfe commit a9665d6

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

Parse/Internal/File/Controller/PFFileController.m

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ - (BFTask *)downloadFileAsyncWithState:(PFFileState *)fileState
113113
cancellationToken:cancellationToken
114114
progressBlock:unifyingProgressBlock];
115115
resultTask = [[resultTask continueWithSuccessBlock:^id(BFTask *task) {
116-
return [[PFFileManager moveItemAsyncAtPath:temporaryPath
117-
toPath:[self cachedFilePathForFileState:fileState]] continueWithBlock:^id(BFTask *task) {
116+
return [[self _cacheFileAsyncWithState:fileState atPath:temporaryPath] continueWithBlock:^id(BFTask *task) {
118117
// Ignore the error if file exists.
119118
if (task.error && task.error.code == NSFileWriteFileExistsError) {
120119
return nil;
@@ -236,25 +235,22 @@ - (BFTask *)uploadFileAsyncWithState:(PFFileState *)fileState
236235
urlString:result.result[@"url"]
237236
mimeType:nil];
238237
return fileState;
239-
}] continueWithSuccessBlock:^id(BFTask *task) {
238+
}] continueWithSuccessBlock:^id(BFTask<PFFileState *> *task) {
240239
@strongify(self);
241-
242-
NSString *finalPath = [self cachedFilePathForFileState:task.result];
243-
NSError *error = nil;
244-
[[NSFileManager defaultManager] moveItemAtPath:sourceFilePath
245-
toPath:finalPath
246-
error:&error];
247-
if (error) {
248-
return [BFTask taskWithError:error];
249-
}
250-
return task;
240+
return [self _cacheFileAsyncWithState:task.result atPath:sourceFilePath];
251241
}];
252242
}
253243

254244
///--------------------------------------
255245
#pragma mark - Cache
256246
///--------------------------------------
257247

248+
- (BFTask<PFVoid> *)_cacheFileAsyncWithState:(PFFileState *)state atPath:(NSString *)path {
249+
return [[PFFileManager createDirectoryIfNeededAsyncAtPath:self.cacheFilesDirectoryPath] continueWithSuccessBlock:^id(BFTask *_) {
250+
return [PFFileManager moveItemAsyncAtPath:path toPath:[self cachedFilePathForFileState:state]];
251+
}];
252+
}
253+
258254
- (NSString *)cachedFilePathForFileState:(PFFileState *)fileState {
259255
if (!fileState.secureURLString) {
260256
return nil;
@@ -266,13 +262,17 @@ - (NSString *)cachedFilePathForFileState:(PFFileState *)fileState {
266262
}
267263

268264
- (NSString *)cacheFilesDirectoryPath {
269-
NSString *path = [self.dataSource.fileManager parseCacheItemPathForPathComponent:PFFileControllerCacheDirectoryName_];
270-
[[PFFileManager createDirectoryIfNeededAsyncAtPath:path] waitForResult:nil withMainThreadWarning:NO];
271-
return path;
265+
return [self.dataSource.fileManager parseCacheItemPathForPathComponent:PFFileControllerCacheDirectoryName_];
272266
}
273267

274268
- (BFTask *)clearFileCacheAsync {
275-
return [PFFileManager removeDirectoryContentsAsyncAtPath:self.cacheFilesDirectoryPath];
269+
return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id{
270+
NSString *path = self.cacheFilesDirectoryPath;
271+
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
272+
return [PFFileManager removeDirectoryContentsAsyncAtPath:path];
273+
}
274+
return nil;
275+
}];
276276
}
277277

278278
@end

0 commit comments

Comments
 (0)