Guest User

Untitled

a guest
May 24th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. //
  2. // OTDownloadOperation.m
  3. // OperationTest
  4. //
  5. // Created by Fraser Speirs on 04/05/2009.
  6. // Copyright 2009 Connected Flow. All rights reserved.
  7. //
  8.  
  9. #import "OTDownloadOperation.h"
  10.  
  11. /*
  12. * This is a 'concurrent' download operation. It starts threads in the background.
  13. */
  14. @implementation OTDownloadOperation
  15.  
  16. - (id)initWithURL:(NSURL *)url {
  17. if(self = [super init]) {
  18. downloadURL = [url retain];
  19. NSLog(@"Created download operation for URL: %@", [downloadURL absoluteString]);
  20. executing = NO;
  21. finished = NO;
  22. }
  23. return self;
  24. }
  25.  
  26. - (void)start {
  27. [self willChangeValueForKey: @"isExecuting"];
  28. executing = YES;
  29. [self didChangeValueForKey: @"isExecuting"];
  30. data = [[NSMutableData data] retain];
  31. myConnection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: downloadURL] delegate: self startImmediately: YES];
  32. }
  33.  
  34. - (BOOL)isConcurrent { return YES; }
  35.  
  36. - (BOOL)isExecuting { return executing; }
  37.  
  38. - (BOOL)isFinished { return finished; }
  39.  
  40. - (void)dealloc {
  41. [downloadURL release];
  42. [data release];
  43. [myConnection release];
  44. [super dealloc];
  45. }
  46.  
  47. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)mdata {
  48. [data appendData: mdata];
  49. }
  50.  
  51. - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
  52. NSLog(@"Finished (%@) %d bytes.", [downloadURL absoluteString], [data length]);
  53. [self updateCompletedState];
  54. }
  55.  
  56. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  57. NSLog(@"Failed (%@)", [downloadURL absoluteString]);
  58. [self updateCompletedState];
  59. }
  60.  
  61. - (void)updateCompletedState {
  62. [self willChangeValueForKey: @"isExecuting"];
  63. executing = NO;
  64. [self didChangeValueForKey: @"isExecuting"];
  65.  
  66. [self willChangeValueForKey: @"isFinished"];
  67. finished = YES;
  68. [self didChangeValueForKey: @"isFinished"];
  69. }
  70. @end
Add Comment
Please, Sign In to add comment