Skip to content

Instantly share code, notes, and snippets.

@dlo
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlo/9f6ef1754aa56f36a15d to your computer and use it in GitHub Desktop.
Save dlo/9f6ef1754aa56f36a15d to your computer and use it in GitHub Desktop.
- (void)cancelLongRunningTasks:(id)sender {
NSDate *date = [NSDate date];
void (^CompletionHandler)(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) = ^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDownloadTask *task in downloadTasks) {
if (task.state == NSURLSessionTaskStateRunning) {
NSDate *startDate = self.sessionDownloadTaskStartDates[@(task.taskIdentifier)];
if (startDate) {
NSTimeInterval interval = [date timeIntervalSinceDate:startDate];
if (interval > 30) {
[task cancel];
}
}
}
}
};
[self.session getTasksWithCompletionHandler:CompletionHandler];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment