Selaa lähdekoodia

Merge pull request #4122 from AFNetworking/merge-fix

Fix analyzer warning for upload task creation
Jeff Kelley 7 vuotta sitten
vanhempi
commit
78ce859962
2 muutettua tiedostoa jossa 27 lisäystä ja 9 poistoa
  1. 12 8
      AFNetworking/AFURLSessionManager.m
  2. 15 1
      Tests/Tests/AFURLSessionManagerTests.m

+ 12 - 8
AFNetworking/AFURLSessionManager.m

@@ -737,17 +737,21 @@ static NSString * const AFNSURLSessionTaskDidSuspendNotification = @"com.alamofi
     __block NSURLSessionUploadTask *uploadTask = nil;
     __block NSURLSessionUploadTask *uploadTask = nil;
     url_session_manager_create_task_safely(^{
     url_session_manager_create_task_safely(^{
         uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
         uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
-    });
-
-    // uploadTask may be nil on iOS7 because uploadTaskWithRequest:fromFile: may return nil despite being documented as nonnull (https://devforums.apple.com/message/926113#926113)
-    if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
-        for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
-            uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
+        
+        // uploadTask may be nil on iOS7 because uploadTaskWithRequest:fromFile: may return nil despite being documented as nonnull (https://devforums.apple.com/message/926113#926113)
+        if (!uploadTask && self.attemptsToRecreateUploadTasksForBackgroundSessions && self.session.configuration.identifier) {
+            for (NSUInteger attempts = 0; !uploadTask && attempts < AFMaximumNumberOfAttemptsToRecreateBackgroundSessionUploadTask; attempts++) {
+                uploadTask = [self.session uploadTaskWithRequest:request fromFile:fileURL];
+            }
         }
         }
+    });
+    
+    if (uploadTask) {
+        [self addDelegateForUploadTask:uploadTask
+                              progress:uploadProgressBlock
+                     completionHandler:completionHandler];
     }
     }
 
 
-    [self addDelegateForUploadTask:uploadTask progress:uploadProgressBlock completionHandler:completionHandler];
-
     return uploadTask;
     return uploadTask;
 }
 }
 
 

+ 15 - 1
Tests/Tests/AFURLSessionManagerTests.m

@@ -37,7 +37,6 @@
 @property (readwrite, nonatomic, strong) AFURLSessionManager *backgroundManager;
 @property (readwrite, nonatomic, strong) AFURLSessionManager *backgroundManager;
 @end
 @end
 
 
-
 @implementation AFURLSessionManagerTests
 @implementation AFURLSessionManagerTests
 
 
 - (NSURLRequest *)bigImageURLRequest {
 - (NSURLRequest *)bigImageURLRequest {
@@ -134,6 +133,21 @@
     [self waitForExpectationsWithCommonTimeout];
     [self waitForExpectationsWithCommonTimeout];
 }
 }
 
 
+// iOS 7 has a bug that may return nil for a session. To simulate that, nil out the
+// session and it will return nil itself.
+- (void)testFileUploadTaskReturnsNilWithBug {
+    [self.localManager setValue:nil forKey:@"session"];
+    
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wnonnull"
+    XCTAssertNil([self.localManager uploadTaskWithRequest:[NSURLRequest requestWithURL:self.baseURL]
+                                                 fromFile:nil
+                                                 progress:NULL
+                                        completionHandler:NULL],
+                 @"Upload task should be nil.");
+#pragma GCC diagnostic pop
+}
+
 - (void)testUploadTaskDoesReportProgress {
 - (void)testUploadTaskDoesReportProgress {
     NSMutableString *payload = [NSMutableString stringWithString:@"AFNetworking"];
     NSMutableString *payload = [NSMutableString stringWithString:@"AFNetworking"];
     while ([payload lengthOfBytesUsingEncoding:NSUTF8StringEncoding] < 20000) {
     while ([payload lengthOfBytesUsingEncoding:NSUTF8StringEncoding] < 20000) {