KVO download file example code

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;
}



你可能感兴趣的:(KVO download file example code)