Defining a Custom Operation Object

摘自:苹果文档

non-concurrrent

#import "LoadOperation.h"

// key for obtaining the current scan count

NSString *kScanCountKey = @"scanCount";

// key for obtaining the path of an image fiel

NSString *kPathKey = @"path";

// key for obtaining the size of an image file

NSString *kSizeKey = @"size";

// key for obtaining the name of an image file

NSString *kNameKey = @"name";

// key for obtaining the mod date of an image file

NSString *kModifiedKey = @"modified";

// NSNotification name to tell the Window controller an image file as found

NSString *kLoadImageDidFinish = @"LoadImageDidFinish";

@interface LoadOperation ()

{

NSURL *loadURL;

NSInteger ourScanCount;

}

@property (retain) NSURL *loadURL;

@end

@implementation LoadOperation

@synthesize loadURL;

// -------------------------------------------------------------------------------

// initWithPath:path

// -------------------------------------------------------------------------------

- (id)initWithURL:(NSURL *)url scanCount:(NSInteger)scanCount

{

self = [super init];

if (self)

{

self.loadURL = url;

ourScanCount = scanCount;

}

return self;

}

// -------------------------------------------------------------------------------

// isImageFile:filePath

//

// Uses LaunchServices and UTIs to detect if a given file path is an image file.

// -------------------------------------------------------------------------------

- (BOOL)isImageFile:(NSURL *)url

{

BOOL isImageFile = NO;

NSString *utiValue;

[url getResourceValue:&utiValue forKey:NSURLTypeIdentifierKey error:nil];

if (utiValue)

{

isImageFile = UTTypeConformsTo((__bridge CFStringRef)utiValue, kUTTypeImage);

}

return isImageFile;

}

// -------------------------------------------------------------------------------

// main:

//

// Examine the given file (from the NSURL "loadURL") to see it its an image file.

// If an image file examine further and report its file attributes.

//

// We could use NSFileManager, but to be on the safe side we will use the

// File Manager APIs to get the file attributes.

// -------------------------------------------------------------------------------

- (void)main

{

if (![self isCancelled])

{

// test to see if it's an image file

if ([self isImageFile:loadURL])

{

// in this example, we just get the file's info (mod date, file size) and report it to the table view

//

NSNumber *fileSize;

[self.loadURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil];

NSDate *fileCreationDate;

[self.loadURL getResourceValue:&fileCreationDate forKey:NSURLCreationDateKey error:nil];

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];

[formatter setTimeStyle:NSDateFormatterNoStyle];

[formatter setDateStyle:NSDateFormatterShortStyle];

NSString *modDateStr = [formatter stringFromDate:fileCreationDate];

NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:

[self.loadURL lastPathComponent], kNameKey,

[self.loadURL absoluteString], kPathKey,

modDateStr, kModifiedKey,

[NSString stringWithFormat:@"%ld", [fileSize integerValue]], kSizeKey,

[NSNumber numberWithInteger:ourScanCount], kScanCountKey, // pass back to check if user cancelled/started a new scan

nil];

if (![self isCancelled])

{

// for the purposes of this sample, we're just going to post the information

// out there and let whoever might be interested receive it (in our case its MyWindowController).

//

[[NSNotificationCenter defaultCenter] postNotificationName:kLoadImageDidFinish object:nil userInfo:info];

}

}

}

}

@end

concurrrent

@interface MyOperation : NSOperation {

BOOL executing;

BOOL finished;

}

- (void)completeOperation;

@end

@implementation MyOperation

- (id)init {

self = [super init];

if (self) {

executing = NO;

finished = NO;

}

return self;

}

- (BOOL)isConcurrent {

return YES;

}

- (BOOL)isExecuting {

return executing;

}

- (BOOL)isFinished {

return finished;

}

@end

*****************

- (void)start {

// Always check for cancellation before launching the task.

if ([self isCancelled])

{

// Must move the operation to the finished state if it is canceled.

[self willChangeValueForKey:@"isFinished"];

finished = YES;

[self didChangeValueForKey:@"isFinished"];

return;

}

// If the operation is not canceled, begin executing the task.

[self willChangeValueForKey:@"isExecuting"];

[NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];

executing = YES;

[self didChangeValueForKey:@"isExecuting"];

}

**************

- (void)main {

@try {

// Do the main work of the operation here.

[self completeOperation];

}

@catch(...) {

// Do not rethrow exceptions.

}

}

- (void)completeOperation {

[self willChangeValueForKey:@"isFinished"];

[self willChangeValueForKey:@"isExecuting"];

executing = NO;

finished = YES;

[self didChangeValueForKey:@"isExecuting"];

[self didChangeValueForKey:@"isFinished"];

}

你可能感兴趣的:(Defining a Custom Operation Object)