Skip to content

Commit

Permalink
Post a notification when finishing moving the downloaded file success…
Browse files Browse the repository at this point in the history
…fully. (AFNetworking#4393)

* Post a notification when finishing moving the downloaded file successfully.

* Add test cases for move notifications.
  • Loading branch information
xingheng committed Mar 27, 2020
1 parent c4d8f62 commit e24d494
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
5 changes: 5 additions & 0 deletions AFNetworking/AFURLSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidSuspendNotification;
*/
FOUNDATION_EXPORT NSString * const AFURLSessionDidInvalidateNotification;

/**
Posted when a session download task finished moving the temporary download file to a specified destination successfully.
*/
FOUNDATION_EXPORT NSString * const AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification;

/**
Posted when a session download task encountered an error when moving the temporary download file to a specified destination.
*/
Expand Down
5 changes: 5 additions & 0 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ static dispatch_group_t url_session_manager_completion_group() {
NSString * const AFNetworkingTaskDidCompleteNotification = @"com.alamofire.networking.task.complete";
NSString * const AFNetworkingTaskDidSuspendNotification = @"com.alamofire.networking.task.suspend";
NSString * const AFURLSessionDidInvalidateNotification = @"com.alamofire.networking.session.invalidate";
NSString * const AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification = @"com.alamofire.networking.session.download.file-manager-succeed";
NSString * const AFURLSessionDownloadTaskDidFailToMoveFileNotification = @"com.alamofire.networking.session.download.file-manager-error";

NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse";
Expand Down Expand Up @@ -324,6 +325,8 @@ - (void)URLSession:(NSURLSession *)session

if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]) {
[[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification object:downloadTask userInfo:nil];
}
}
}
Expand Down Expand Up @@ -1210,6 +1213,8 @@ - (void)URLSession:(NSURLSession *)session

if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error]) {
[[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo];
} else {
[[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification object:downloadTask userInfo:nil];
}

return;
Expand Down
37 changes: 37 additions & 0 deletions Tests/Tests/AFURLSessionManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,43 @@ - (void)testBackgroundManagerReturnsExpectedClassForDownloadTask {
}
}

#pragma mark - Notifications

- (void)testTaskMoveSuccessfullyAfterDownloading {
NSURL *dirURL = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *destinationURL = [dirURL URLByAppendingPathComponent:NSUUID.UUID.UUIDString];

NSURLSessionDownloadTask *task = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest]
progress:nil
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
return destinationURL;
}
completionHandler:nil];

[self expectationForNotification:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification
object:nil
handler:nil];
[task resume];
[self waitForExpectationsWithCommonTimeout];
[[NSFileManager defaultManager] removeItemAtURL:destinationURL error:nil];
}

- (void)testTaskMoveFailedAfterDownloading {
NSURLSessionDownloadTask *downloadTask = [self.localManager downloadTaskWithRequest:[self bigImageURLRequest]
progress:nil
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
// Try to move the destination file to a nonexist path on purpose for simulating a move failure.
return [NSURL fileURLWithPath:@"/a/b/c"];
}
completionHandler:nil];

[self expectationForNotification:AFURLSessionDownloadTaskDidFailToMoveFileNotification
object:nil
handler:nil];
[downloadTask resume];
[self waitForExpectationsWithCommonTimeout];
}

#pragma mark - private

- (void)_testResumeNotificationForTask:(NSURLSessionTask *)task {
Expand Down

0 comments on commit e24d494

Please sign in to comment.