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"];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市羞迷,隨后出現(xiàn)的幾起案子腋妙,更是在濱河造成了極大的恐慌镊尺,老刑警劉巖后德,帶你破解...
    沈念sama閱讀 211,290評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異折晦,居然都是意外死亡货抄,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,107評論 2 385
  • 文/潘曉璐 我一進店門蒲跨,熙熙樓的掌柜王于貴愁眉苦臉地迎上來译断,“玉大人,你說我怎么就攤上這事或悲∷镞洌” “怎么了堪唐?”我有些...
    開封第一講書人閱讀 156,872評論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長翎蹈。 經常有香客問我淮菠,道長,這世上最難降的妖魔是什么荤堪? 我笑而不...
    開封第一講書人閱讀 56,415評論 1 283
  • 正文 為了忘掉前任合陵,我火速辦了婚禮,結果婚禮上澄阳,老公的妹妹穿的比我還像新娘拥知。我一直安慰自己,他們只是感情好碎赢,可當我...
    茶點故事閱讀 65,453評論 6 385
  • 文/花漫 我一把揭開白布低剔。 她就那樣靜靜地躺著,像睡著了一般肮塞。 火紅的嫁衣襯著肌膚如雪户侥。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,784評論 1 290
  • 那天峦嗤,我揣著相機與錄音蕊唐,去河邊找鬼。 笑死烁设,一個胖子當著我的面吹牛替梨,可吹牛的內容都是我干的。 我是一名探鬼主播装黑,決...
    沈念sama閱讀 38,927評論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼副瀑,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了恋谭?” 一聲冷哼從身側響起糠睡,我...
    開封第一講書人閱讀 37,691評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎疚颊,沒想到半個月后狈孔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 44,137評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡材义,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,472評論 2 326
  • 正文 我和宋清朗相戀三年均抽,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片其掂。...
    茶點故事閱讀 38,622評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡油挥,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情深寥,我是刑警寧澤攘乒,帶...
    沈念sama閱讀 34,289評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站惋鹅,受9級特大地震影響持灰,放射性物質發(fā)生泄漏。R本人自食惡果不足惜负饲,卻給世界環(huán)境...
    茶點故事閱讀 39,887評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喂链。 院中可真熱鬧返十,春花似錦、人聲如沸椭微。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蝇率。三九已至迟杂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間本慕,已是汗流浹背排拷。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評論 1 265
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留锅尘,地道東北人监氢。 一個月前我還...
    沈念sama閱讀 46,316評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像藤违,于是被迫代替她去往敵國和親浪腐。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,490評論 2 348

推薦閱讀更多精彩內容