Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // OTDownloadOperation.m
- // OperationTest
- //
- // Created by Fraser Speirs on 04/05/2009.
- // Copyright 2009 Connected Flow. All rights reserved.
- //
- #import "OTDownloadOperation.h"
- /*
- * This is a 'concurrent' download operation. It starts threads in the background.
- */
- @implementation OTDownloadOperation
- - (id)initWithURL:(NSURL *)url {
- if(self = [super init]) {
- downloadURL = [url retain];
- NSLog(@"Created download operation for URL: %@", [downloadURL absoluteString]);
- executing = NO;
- finished = NO;
- }
- return self;
- }
- - (void)start {
- [self willChangeValueForKey: @"isExecuting"];
- executing = YES;
- [self didChangeValueForKey: @"isExecuting"];
- data = [[NSMutableData data] retain];
- myConnection = [[NSURLConnection alloc] initWithRequest: [NSURLRequest requestWithURL: downloadURL] delegate: self startImmediately: YES];
- }
- - (BOOL)isConcurrent { return YES; }
- - (BOOL)isExecuting { return executing; }
- - (BOOL)isFinished { return finished; }
- - (void)dealloc {
- [downloadURL release];
- [data release];
- [myConnection release];
- [super dealloc];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)mdata {
- [data appendData: mdata];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- NSLog(@"Finished (%@) %d bytes.", [downloadURL absoluteString], [data length]);
- [self updateCompletedState];
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- NSLog(@"Failed (%@)", [downloadURL absoluteString]);
- [self updateCompletedState];
- }
- - (void)updateCompletedState {
- [self willChangeValueForKey: @"isExecuting"];
- executing = NO;
- [self didChangeValueForKey: @"isExecuting"];
- [self willChangeValueForKey: @"isFinished"];
- finished = YES;
- [self didChangeValueForKey: @"isFinished"];
- }
- @end
Add Comment
Please, Sign In to add comment