Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. self.session = [self backgroundSession];
  2.  
  3. - (NSURLSession *)backgroundSession
  4. {
  5. static NSURLSession *session = nil;
  6. static dispatch_once_t onceToken;
  7. dispatch_once(&onceToken, ^{
  8. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.uploadSession"];
  9. configuration.HTTPMaximumConnectionsPerHost = 1;
  10. session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  11. });
  12. return session;
  13. }
  14.  
  15. - (void)applicationDidEnterBackground:(UIApplication *)application
  16. {
  17. [self uploadPossibleDrives];
  18. }
  19.  
  20. // Start uploading the remaing gps log in th right order:
  21.  
  22. NSOperationQueue *queue = [[NSOperationQueue alloc] init];
  23. [queue setMaxConcurrentOperationCount:1];
  24.  
  25. for (int i = 0; i < arrayOfGPSLogChunks.count; i++)
  26. {
  27.  
  28. // This will ensure the chunks are sent in the right order
  29. // Add an operation as a block to a queue:
  30.  
  31. [queue addOperationWithBlock: ^ {
  32.  
  33. NSData *requestData;
  34. NSMutableURLRequest *request;
  35.  
  36. if (i == 0)
  37. {
  38. NSLog(@"Initial drive upload %i",ID.integerValue);
  39.  
  40. requestData = [NSJSONSerialization dataWithJSONObject:historyToUpload options:NSJSONWritingPrettyPrinted error:nil];
  41. request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kInitialUploadDriveURL];
  42. [request setHTTPMethod:@"POST"];
  43. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  44. [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
  45.  
  46. }
  47. else
  48. {
  49. NSLog(@"Chunk %i",i);
  50.  
  51. NSMutableDictionary *chunk = [[NSMutableDictionary alloc] init];
  52. [chunk setObject:driveID forKey:@"driveID"];
  53. [chunk setObject:[arrayOfGPSLogChunks objectAtIndex:i] forKey:@"gpsLog"];
  54.  
  55. requestData = [NSJSONSerialization dataWithJSONObject:chunk options:NSJSONWritingPrettyPrinted error:nil];
  56. request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kUploadDrivesChunkURL]];
  57. [request setHTTPMethod:@"POST"];
  58. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  59. [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
  60.  
  61.  
  62. }
  63.  
  64. NSLog(@"_session: %@",self.session);
  65. NSLog(@"request: %@",request);
  66. NSLog(@"requestData: %lu",(unsigned long)requestData.length);
  67. NSLog(@"uploadTask: %@",self.uploadTask);
  68.  
  69. self.uploadTask = [self.session uploadTaskWithRequest:request fromData:requestData];
  70. [self.uploadTask resume];
  71.  
  72. if (i == arrayOfGPSLogChunks.count-1)
  73. {
  74. // All gps logs were uploaded so now we save its state as 'uploaded to server':
  75. NSLog(@"Finished sending to server!");
  76. [self setSentToServerForDriveID:ID];
  77. }
  78.  
  79. }];
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement