KVO download file example code

5109 단어
MyOperation
#import <Foundation/Foundation.h>
@interface MyOperation : NSOperation{
	NSURLRequest *_request;
	NSURLConnection *_connection;
	NSMutableData *_data;
	BOOL isFinished;
	NSURL *_url;
}

-(id)initWithURLString:(NSString *)url;
-(NSData *)data;
-(NSURL *)url;

@end;

#import <Foundation/Foundation.h>
#import "MyOperation.h"
@implementation MyOperation
-(void)dealloc{
    [_request release],_request=nil;
    [_data release],_data=nil;
    [_connection release],_connection=nil;
    [_url release], _url = nil;	
    [super dealloc];
}

-(NSData *)data{
	return _data;
}

-(NSURL *)url{
	return _url;
}

-(id)initWithURLString:(NSString *)aURL{
	self = [super init];
	if (self){
		NSURL *bURL = [NSURL URLWithString:aURL];
		_request = [[NSURLRequest alloc] initWithURL:bURL];
		_data = [[NSMutableData alloc] init];
		_url = bURL;
	}
	return self;
}

- (BOOL)isFinished{
	return isFinished;
}

//          ,          .
- (BOOL) isConcurrent{
	return YES;//  yes        ,         
}


/*           start  。Start NSOperation     ,               ,  
 *        , NSOperation         (         ),           ,
 *             (  delegate  )      。      start           
 *
 *
*/
-(void) start{
	if (![self isCancelled]){
		NSLog(@"start operation");
		_connection = [[NSURLConnection connectionWithRequest:_request delegate:self] retain];
		//              ,         ,
		//  connection delegate       ,                 
		while(_connection != nil){
			[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];    
		}
	}
	//[self setValue:[NSNumber numberWithBool:YES] forKey:@"isFinished"];
}


+ (BOOL) automaticallyNotifiesObserversForKey: (NSString*)aKey{
	NSLog(@"Called automaticallyNotifiesObserversForKey function.");//KVO 
	if ([aKey isEqualToString:@"isFinished"]){
		return YES;
	}
	return [super automaticallyNotifiesObserversForKey:aKey];
}



-(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
    NSLog(@"connection:");
    [_data appendData:data];
 
}

// HTTP     
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
    [_connection release],_connection=nil;
    [self setValue:[NSNumber numberWithBool:YES] forKey:@"isFinished"];
}

-(void)connection: (NSURLConnection *) connection didFailWithError: (NSError *) error{
    NSLog(@"connection error");
    [self setValue:[NSNumber numberWithBool:YES] forKey:@"isFinished"];
}

@end;

CallOperation
#import <Foundation/Foundation.h>
#import "MyOperation.h"

@interface CallOperation : NSObject{

}
-(void)call;
@end;

#import <Foundation/Foundation.h>
#import "MyOperation.h"
#import "CallOperation.h"

@implementation CallOperation

-(void)call{

}


- (void) observeValueForKeyPath: (NSString*)aPath
		       ofObject: (id)anObject
			 change: (NSDictionary*)aChange
		        context: (void*)aContext{
			
	if ([aPath isEqualToString:@"isFinished"]){
		BOOL isFinished=[[aChange objectForKey:NSKeyValueChangeNewKey] intValue];
		if (isFinished){
			NSLog(@"call observeValueForKeyPath function.");
			MyOperation *opr = (MyOperation *)aContext;


			NSURL *url = [opr url];
			NSString *fileName = [[url absoluteString] lastPathComponent];
			if (!fileName){
				fileName = @"index.html";
			}

			//NSLog(@"data is: %@", [opr data]);
			[[opr data] writeToFile:fileName atomically:YES];
			//  kvo  
			//[opr removeObserver:self forKeyPath:@"isFinished"];
			//NSLog(@"removeObserver:forKeyPath");
		}


	}else{
		[super observeValueForKeyPath:aPath
                             ofObject:anObject
                               change:aChange
                              context:aContext];

	}
			
}

@end;

main
#import <Foundation/Foundation.h>
#import "MyOperation.h"
#import "CallOperation.h"


int main (int argc, const char *argv[]) {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	CallOperation *call = [[CallOperation alloc] init];

	NSOperationQueue *queue = [[NSOperationQueue alloc] init];	
	MyOperation *operation1 = [[MyOperation alloc] initWithURLString:@"http://www.eet-china.com/"];

	[operation1 addObserver:call 
		     forKeyPath:@"isFinished"
			options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
			context:operation1];
	[queue addOperation:operation1];


	
	MyOperation *operation2 = [[MyOperation alloc] initWithURLString:@"http://www.eet-china.com/ART_8800646672_617693_NT_f0b705ea.HTM"];
	
	[operation2 addObserver:call 
		     forKeyPath:@"isFinished"
			options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
			context:operation2];
	[queue addOperation:operation2];
	
	

	
	[NSThread sleepForTimeInterval:80];//       80 
	[operation1 release];
	[operation2 release];
	[call release];
	[queue release];

	

	[pool drain];
	return 0;
}

좋은 웹페이지 즐겨찾기