Преглед изворни кода

Post a notification when finishing moving the downloaded file successfully. (#4393)

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

* Add test cases for move notifications.
Will Han пре 5 година
родитељ
комит
e24d494eff

+ 5 - 0
AFNetworking/AFURLSessionManager.h

@@ -509,6 +509,11 @@ FOUNDATION_EXPORT NSString * const AFNetworkingTaskDidSuspendNotification;
  */
  */
 FOUNDATION_EXPORT NSString * const AFURLSessionDidInvalidateNotification;
 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.
  Posted when a session download task encountered an error when moving the temporary download file to a specified destination.
  */
  */

+ 5 - 0
AFNetworking/AFURLSessionManager.m

@@ -46,6 +46,7 @@ NSString * const AFNetworkingTaskDidResumeNotification = @"com.alamofire.network
 NSString * const AFNetworkingTaskDidCompleteNotification = @"com.alamofire.networking.task.complete";
 NSString * const AFNetworkingTaskDidCompleteNotification = @"com.alamofire.networking.task.complete";
 NSString * const AFNetworkingTaskDidSuspendNotification = @"com.alamofire.networking.task.suspend";
 NSString * const AFNetworkingTaskDidSuspendNotification = @"com.alamofire.networking.task.suspend";
 NSString * const AFURLSessionDidInvalidateNotification = @"com.alamofire.networking.session.invalidate";
 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 AFURLSessionDownloadTaskDidFailToMoveFileNotification = @"com.alamofire.networking.session.download.file-manager-error";
 
 
 NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse";
 NSString * const AFNetworkingTaskDidCompleteSerializedResponseKey = @"com.alamofire.networking.task.complete.serializedresponse";
@@ -324,6 +325,8 @@ didFinishDownloadingToURL:(NSURL *)location
 
 
             if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]) {
             if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:self.downloadFileURL error:&fileManagerError]) {
                 [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo];
                 [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:fileManagerError.userInfo];
+            } else {
+                [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification object:downloadTask userInfo:nil];
             }
             }
         }
         }
     }
     }
@@ -1210,6 +1213,8 @@ didFinishDownloadingToURL:(NSURL *)location
             
             
             if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error]) {
             if (![[NSFileManager defaultManager] moveItemAtURL:location toURL:fileURL error:&error]) {
                 [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo];
                 [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidFailToMoveFileNotification object:downloadTask userInfo:error.userInfo];
+            } else {
+                [[NSNotificationCenter defaultCenter] postNotificationName:AFURLSessionDownloadTaskDidMoveFileSuccessfullyNotification object:downloadTask userInfo:nil];
             }
             }
 
 
             return;
             return;

+ 37 - 0
Tests/Tests/AFURLSessionManagerTests.m

@@ -442,6 +442,43 @@
     }
     }
 }
 }
 
 
+#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
 #pragma mark - private
 
 
 - (void)_testResumeNotificationForTask:(NSURLSessionTask *)task {
 - (void)_testResumeNotificationForTask:(NSURLSessionTask *)task {