|
@@ -37,12 +37,6 @@ typedef NS_ENUM(NSInteger, AFOperationState) {
|
|
AFOperationFinishedState = 3,
|
|
AFOperationFinishedState = 3,
|
|
};
|
|
};
|
|
|
|
|
|
-#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && !defined(AF_APP_EXTENSIONS)
|
|
|
|
-typedef UIBackgroundTaskIdentifier AFBackgroundTaskIdentifier;
|
|
|
|
-#else
|
|
|
|
-typedef id AFBackgroundTaskIdentifier;
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
static dispatch_group_t url_request_operation_completion_group() {
|
|
static dispatch_group_t url_request_operation_completion_group() {
|
|
static dispatch_group_t af_url_request_operation_completion_group;
|
|
static dispatch_group_t af_url_request_operation_completion_group;
|
|
static dispatch_once_t onceToken;
|
|
static dispatch_once_t onceToken;
|
|
@@ -72,6 +66,7 @@ typedef void (^AFURLConnectionOperationProgressBlock)(NSUInteger bytes, long lon
|
|
typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge);
|
|
typedef void (^AFURLConnectionOperationAuthenticationChallengeBlock)(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge);
|
|
typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse);
|
|
typedef NSCachedURLResponse * (^AFURLConnectionOperationCacheResponseBlock)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse);
|
|
typedef NSURLRequest * (^AFURLConnectionOperationRedirectResponseBlock)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse);
|
|
typedef NSURLRequest * (^AFURLConnectionOperationRedirectResponseBlock)(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse);
|
|
|
|
+typedef void (^AFURLConnectionOperationBackgroundTaskCleanupBlock)();
|
|
|
|
|
|
static inline NSString * AFKeyPathFromOperationState(AFOperationState state) {
|
|
static inline NSString * AFKeyPathFromOperationState(AFOperationState state) {
|
|
switch (state) {
|
|
switch (state) {
|
|
@@ -144,7 +139,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
|
|
@property (readwrite, nonatomic, copy) NSString *responseString;
|
|
@property (readwrite, nonatomic, copy) NSString *responseString;
|
|
@property (readwrite, nonatomic, assign) NSStringEncoding responseStringEncoding;
|
|
@property (readwrite, nonatomic, assign) NSStringEncoding responseStringEncoding;
|
|
@property (readwrite, nonatomic, assign) long long totalBytesRead;
|
|
@property (readwrite, nonatomic, assign) long long totalBytesRead;
|
|
-@property (readwrite, nonatomic, assign) AFBackgroundTaskIdentifier backgroundTaskIdentifier;
|
|
|
|
|
|
+@property (readwrite, nonatomic, copy) AFURLConnectionOperationBackgroundTaskCleanupBlock backgroundTaskCleanup;
|
|
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress;
|
|
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress;
|
|
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress;
|
|
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress;
|
|
@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge;
|
|
@property (readwrite, nonatomic, copy) AFURLConnectionOperationAuthenticationChallengeBlock authenticationChallenge;
|
|
@@ -214,13 +209,10 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
|
|
[_outputStream close];
|
|
[_outputStream close];
|
|
_outputStream = nil;
|
|
_outputStream = nil;
|
|
}
|
|
}
|
|
-
|
|
|
|
-#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && !defined(AF_APP_EXTENSIONS)
|
|
|
|
- if (_backgroundTaskIdentifier) {
|
|
|
|
- [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskIdentifier];
|
|
|
|
- _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
|
|
+
|
|
|
|
+ if (_backgroundTaskCleanup) {
|
|
|
|
+ _backgroundTaskCleanup();
|
|
}
|
|
}
|
|
-#endif
|
|
|
|
}
|
|
}
|
|
|
|
|
|
#pragma mark -
|
|
#pragma mark -
|
|
@@ -293,13 +285,22 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
|
|
[self.lock unlock];
|
|
[self.lock unlock];
|
|
}
|
|
}
|
|
|
|
|
|
-#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && !defined(AF_APP_EXTENSIONS)
|
|
|
|
|
|
+#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
|
|
- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler {
|
|
- (void)setShouldExecuteAsBackgroundTaskWithExpirationHandler:(void (^)(void))handler {
|
|
[self.lock lock];
|
|
[self.lock lock];
|
|
- if (!self.backgroundTaskIdentifier) {
|
|
|
|
|
|
+ if (!self.backgroundTaskCleanup) {
|
|
UIApplication *application = [UIApplication sharedApplication];
|
|
UIApplication *application = [UIApplication sharedApplication];
|
|
|
|
+ UIBackgroundTaskIdentifier __block backgroundTaskIdentifier = UIBackgroundTaskInvalid;
|
|
__weak __typeof(self)weakSelf = self;
|
|
__weak __typeof(self)weakSelf = self;
|
|
- self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
|
|
|
|
|
|
+
|
|
|
|
+ self.backgroundTaskCleanup = ^(){
|
|
|
|
+ if (backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
|
|
|
|
+ [[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
|
|
|
|
+ backgroundTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
|
|
__strong __typeof(weakSelf)strongSelf = weakSelf;
|
|
__strong __typeof(weakSelf)strongSelf = weakSelf;
|
|
|
|
|
|
if (handler) {
|
|
if (handler) {
|
|
@@ -308,9 +309,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
|
|
|
|
|
|
if (strongSelf) {
|
|
if (strongSelf) {
|
|
[strongSelf cancel];
|
|
[strongSelf cancel];
|
|
-
|
|
|
|
- [application endBackgroundTask:strongSelf.backgroundTaskIdentifier];
|
|
|
|
- strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
|
|
+ strongSelf.backgroundTaskCleanup();
|
|
}
|
|
}
|
|
}];
|
|
}];
|
|
}
|
|
}
|