Last active
October 12, 2016 02:06
-
-
Save claybridges/8ca22217830e0ce1a0f1 to your computer and use it in GitHub Desktop.
Just an easier format for code from this SO question (not mine): http://stackoverflow.com/questions/25311736.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) addTasksToSessionWithTaskObject:(Task*)taskObject withSessionInitialisationNeeded:(BOOL) needed{ | |
NSString *filePath = [[NSBundle mainBundle] pathForResource:pathForResourceFile ofType:resourceFileType]; | |
S3PutObjectRequest *putObjectRequest = [[S3PutObjectRequest alloc] initWithKey:targetFileKey | |
inBucket:_bucketname]; | |
putObjectRequest.cannedACL = [S3CannedACL publicReadWrite]; | |
putObjectRequest.filename = filePath; | |
putObjectRequest.contentType = [resourceFileType isEqualToString:@"MOV"] ? @"movie/mov" : @"image/jpg"; | |
putObjectRequest.endpoint = @"http://s3.amazonaws.com"; | |
putObjectRequest.contentLength=[[[NSFileManager defaultManager] | |
attributesOfItemAtPath:filePath error:nil] fileSize]; | |
putObjectRequest.delegate = self; | |
[putObjectRequest configureURLRequest]; | |
NSMutableURLRequest *request = [s3Client signS3Request:putObjectRequest]; | |
NSMutableURLRequest *request2 = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://s3.amazonaws.com/UploadTest/%@",taskObject.fileKey]]]; | |
[request2 setHTTPMethod:request.HTTPMethod]; | |
[request2 setAllHTTPHeaderFields:[request allHTTPHeaderFields]]; | |
if(needed) { | |
sharedSession = [self backgroundSession]; | |
[sharedSession setAttemptsToRecreateUploadTasksForBackgroundSessions:YES]; | |
sharedSession.securityPolicy = securityPolicy; | |
[sharedSession setSessionDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition (NSURLSession *session, NSURLAuthenticationChallenge *challenge, NSURLCredential * __autoreleasing *credential) { | |
NSLog(@"%@ %@ %ld", challenge.protectionSpace,challenge.proposedCredential,(long)challenge.previousFailureCount); | |
return NSURLSessionAuthChallengePerformDefaultHandling; | |
}]; | |
[sharedSession setTaskDidCompleteBlock:^(NSURLSession *sessionI, NSURLSessionTask *task, NSError *error) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
__block UIBackgroundTaskIdentifier bgTaskI = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{ | |
[[UIApplication sharedApplication] endBackgroundTask:bgTaskI]; | |
}]; | |
if([currentlyActiveTaskIdArray containsObject:@([task taskIdentifier])]){ | |
[currentlyActiveTaskIdArray removeObject:@([task taskIdentifier])]; | |
} | |
if(currentlyActiveTaskIdArray.count < LOWER_SLAB_FOR_TASKS){ | |
[self LogMessage:[NSString stringWithFormat: @"Lower Slab For Task Reached-ADDING MORE TASKS"]]; | |
[self initiateS3UploadForSetOfTasksIsItBeginningOfUpload:NO]; | |
} | |
[[UIApplication sharedApplication] endBackgroundTask:bgTaskI]; | |
}); | |
}]; | |
[sharedSession setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession *sessionI) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
AppDelegate * delegate =(AppDelegate *)[[UIApplication sharedApplication] delegate]; | |
if(delegate.backgroundSessionCompletionHandler){ | |
void (^handler)() = delegate.backgroundSessionCompletionHandler; | |
NSLog(@"App going to be put back to sleep"); | |
handler(); | |
} | |
}); | |
}]; | |
} | |
NSURLSessionUploadTask *task = [sharedSession uploadTaskWithRequest:request2 fromFile:taskObject.fileURL progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) { | |
NSLog(@"Complted Task with %@ %@ %@", error,responseObject,response); | |
}]; | |
task.taskDescription= taskObject.fileURL; | |
[currentlyActiveTaskIdArray addObject:@([task taskIdentifier])]; | |
} | |
// Heres my code to add more tasks | |
- (void)initiateS3UploadForSetOfTasksIsItBeginningOfUpload:(BOOL)beginning{ | |
int i=0; | |
for(Task *eachTaskObject in tasksArray){ | |
if(i < numberOfTasksTobeAdded){ | |
[self addTasksToSessionWithTaskObject:eachTaskObject WithSessionInitialisationNeeded:NO]; | |
i++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment