Skip to content

Commit f84976c

Browse files
committed
Added an ability to download a file and get a file path to PFFiles.
1 parent fe978af commit f84976c

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

Parse/PFConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ typedef void (^PFSetResultBlock)(NSSet *PF_NULLABLE_S channels, NSError *PF_NULL
381381
typedef void (^PFUserResultBlock)(PFUser *PF_NULLABLE_S user, NSError *PF_NULLABLE_S error);
382382
typedef void (^PFDataResultBlock)(NSData *PF_NULLABLE_S data, NSError *PF_NULLABLE_S error);
383383
typedef void (^PFDataStreamResultBlock)(NSInputStream *PF_NULLABLE_S stream, NSError *PF_NULLABLE_S error);
384+
typedef void (^PFFilePathResultBlock)(NSString *PF_NULLABLE_S filePath, NSError *PF_NULLABLE_S error);
384385
typedef void (^PFStringResultBlock)(NSString *PF_NULLABLE_S string, NSError *PF_NULLABLE_S error);
385386
typedef void (^PFIdResultBlock)(PF_NULLABLE_S id object, NSError *PF_NULLABLE_S error);
386387
typedef void (^PFProgressBlock)(int percentDone);

Parse/PFFile.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,38 @@ PF_ASSUME_NONNULL_BEGIN
374374
*/
375375
- (void)getDataInBackgroundWithTarget:(PF_NULLABLE_S id)target selector:(PF_NULLABLE_S SEL)selector;
376376

377+
/*!
378+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
379+
380+
@returns The task, with the result set to `NSString` representation of a file path.
381+
*/
382+
- (BFTask PF_GENERIC(NSString *) *)getFileInBackground;
383+
384+
/*!
385+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
386+
387+
@param progressBlock The block should have the following argument signature: `^(int percentDone)`.
388+
389+
@returns The task, with the result set to `NSString` representation of a file path.
390+
*/
391+
- (BFTask PF_GENERIC(NSString *) *)getFileInBackgroundWithProgressBlock:(PF_NULLABLE PFProgressBlock)progressBlock;
392+
393+
/*!
394+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
395+
396+
@param block The block should have the following argument signature: `^(NSString *filePath, NSError *error)`.
397+
*/
398+
- (void)getFileInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block;
399+
400+
/*!
401+
@abstract *Asynchronously* gets the file path for file from cache if available or fetches its contents from the network.
402+
403+
@param block The block should have the following argument signature: `^(NSString *filePath, NSError *error)`.
404+
@param progressBlock The block should have the following argument signature: `^(int percentDone)`.
405+
*/
406+
- (void)getFileInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block
407+
progressBlock:(PF_NULLABLE PFProgressBlock)progressBlock;
408+
377409
///--------------------------------------
378410
/// @name Interrupting a Transfer
379411
///--------------------------------------

Parse/PFFile.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,28 @@ - (void)getDataInBackgroundWithTarget:(id)target selector:(SEL)selector {
213213
}];
214214
}
215215

216+
- (BFTask PF_GENERIC(NSString *) *)getFileInBackground {
217+
return [self getFileInBackgroundWithProgressBlock:nil];
218+
}
219+
220+
- (BFTask PF_GENERIC(NSString *)*)getFileInBackgroundWithProgressBlock:(PFProgressBlock)progressBlock {
221+
return [[self _downloadAsyncWithProgressBlock:progressBlock] continueWithSuccessBlock:^id(BFTask *task) {
222+
if (self.dirty) {
223+
return self.stagedFilePath;
224+
}
225+
return [[[self class] fileController] cachedFilePathForFileState:self.state];
226+
}];
227+
}
228+
229+
- (void)getFileInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block {
230+
[[self getFileInBackground] thenCallBackOnMainThreadAsync:block];
231+
}
232+
233+
- (void)getFileInBackgroundWithBlock:(PF_NULLABLE PFFilePathResultBlock)block
234+
progressBlock:(PF_NULLABLE PFProgressBlock)progressBlock {
235+
[[self getFileInBackgroundWithProgressBlock:progressBlock] thenCallBackOnMainThreadAsync:block];
236+
}
237+
216238
#pragma mark Interrupting
217239

218240
- (void)cancel {

Tests/Unit/FileUnitTests.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,42 @@ - (void)testDownloading{
415415
return nil;
416416
}];
417417

418+
wait_next;
419+
expectation = [self expectationForSelector:@selector(getFileInBackground)];
420+
[[file getFileInBackground] continueWithBlock:^id(BFTask *task) {
421+
NSData *data = [NSData dataWithContentsOfFile:task.result];
422+
XCTAssertEqualObjects(data, expectedData);
423+
[expectation fulfill];
424+
return nil;
425+
}];
426+
427+
wait_next;
428+
expectation = [self expectationForSelector:@selector(getFileInBackgroundWithProgressBlock:)];
429+
[[file getFileInBackgroundWithProgressBlock:[self progressValidationBlock]] continueWithBlock:^id(BFTask *task) {
430+
NSData *data = [NSData dataWithContentsOfFile:task.result];
431+
XCTAssertEqualObjects(data, expectedData);
432+
[expectation fulfill];
433+
return nil;
434+
}];
435+
436+
wait_next;
437+
expectation = [self expectationForSelector:@selector(getFileInBackgroundWithBlock:)];
438+
[file getFileInBackgroundWithBlock:^(NSString *filePath, NSError *error) {
439+
XCTAssertNil(error);
440+
NSData *data = [NSData dataWithContentsOfFile:filePath];
441+
XCTAssertEqualObjects(data, expectedData);
442+
[expectation fulfill];
443+
}];
444+
445+
wait_next;
446+
expectation = [self expectationForSelector:@selector(getFileInBackgroundWithBlock:progressBlock:)];
447+
[file getFileInBackgroundWithBlock:^(NSString *filePath, NSError *error) {
448+
XCTAssertNil(error);
449+
NSData *data = [NSData dataWithContentsOfFile:filePath];
450+
XCTAssertEqualObjects(data, expectedData);
451+
[expectation fulfill];
452+
} progressBlock:[self progressValidationBlock]];
453+
418454
wait_next;
419455
}
420456

0 commit comments

Comments
 (0)