daisy NSOperation main 变成 super 是否可以接受?
Is it acceptable to daisy NSOperation main into super?
- (void)main
{
IDBAssert0(self.bestCapture.webpCandidate);
self.finished = NO;
self.executing = YES;
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
UIImage *possiblycorrupted = [UIImage imageWithWebPData:self.bestCapture.webpCandidate];
NSTimeInterval webpInterval = [NSDate timeIntervalSinceReferenceDate]-now;
NSDLog(@"it tooke %.2f sec to unpack webp", webpInterval);
self.microblinkCandidate = possiblycorrupted; // data superclass nsoperation processes
[super main];
}
基础 class 中的第一件事自然是将 finished 设置为 no 并将 execution 设置为 yes:
- (void)main
{
self.finished = NO;
self.executing = YES;
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
start = now;
CGSize size = [self.microblinkCandidate size];
IDBAssert0(size.width && size.height);
IDBAssert0(self.microblink);
// this starts async processing
[self.microblink processImage:self.microblinkCandidate
scanningRegion:CGRectMake(0.0, 0.0, 1.0, 1.0)
delegate:self];
while (![self isCancelled])
{
sleep(1);
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if(now - start > 5) {
// #5677 microblink watchdog to detect hangs
[self cancel];
break;
}
}
[self done];
}
因为它不是摘要,也将单独使用。
循环仅用于 debug/watchdog 目的
在正常操作中它没有跳闸操作完成
如果这个回调:
- (void)scanningViewController: (UIViewController<PPScanningViewController>*)scanningViewController
didOutputResults:(NSArray*)results
{
if([results count]>0) {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSDLog(@"found barcode in %.1fs", now - start);
self.microblinkSuccessHandler();
}else{
IDBAssert0(self.microblinkFailureHandler);
self.microblinkFailureHandler();
}
[self done];
}
在 "processImage:" 完成时调用(及时)。
最基本的class是
@implementation IDBAsynchronousOperation
@synthesize executing = _executing;
@synthesize finished = _finished;
-(BOOL)isFinished
{
return _finished;
}
- (void)setFinished:(BOOL)finished
{
[self willChangeValueForKey:@"isFinished"];
_finished = finished;
[self didChangeValueForKey:@"isFinished"];
}
-(BOOL)isExecuting
{
return _executing;
}
- (void)setExecuting:(BOOL)executing
{
[self willChangeValueForKey:@"isExecuting"];
_executing = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (instancetype)init
{
self = [super init];
if (self) {
// self.completionBlock = ^{
// NSDLog(@"image barcode search has finished");
// };
IDBAssert0(sizeof(_executing)<2);
}
return self;
}
-(BOOL)isAsynchronous
{
return YES;
}
@end
您当然可以(我们经常这样做)subclass 您自己的具体 NSOperation
subclass。
要使基础 class 成为子class,您需要确保只执行一次 self.executing = true
。现在,base class 和 subclass 中的 main
都会执行此操作,因此您将执行两次。典型的解决方案是将它从这两个 main
实现中拉出来,并在基础 class 的 start
中进行。无论如何,Apple 建议您在 start
中执行此操作。
因此从两个 main
实现中删除了 self.finished
和 self.executing
内容,然后您可以实现 start
:
- (void)start {
if ([self isCancelled]) {
self.finished = YES;
return;
}
self.executing = YES;
[self main];
}
请注意,您不必在操作开始时调用 self.finished = false
,因为那样会发送不必要的 KVO。
一个不相关的观察:
如果您在基础 class 中保留 while
循环,如果 [self isCancelled]
或者 processImage
委托完成方法是调用(也许您可以更新一些状态 属性 以指定调用该委托方法的时间)。现在,如果 processImage
在超时之前完成,它将保持操作 运行 整整 5 秒。
就我个人而言,根据 processImage
的设计方式,我可能倾向于完全删除 while
循环。您通常希望完全避免任何这样的轮询。例如,我可能会将 [self done]
放在适当的委托方法中,然后设置一个计时器或 dispatch_after
超时。
- (void)main {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
start = now;
CGSize size = [self.microblinkCandidate size];
IDBAssert0(size.width && size.height);
IDBAssert0(self.microblink);
// this starts async processing
[self.microblink processImage:self.microblinkCandidate
scanningRegion:CGRectMake(0.0, 0.0, 1.0, 1.0)
delegate:self];
// cancel upon timeout
typeof(self) __weak weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
typeof(self) __strong strongSelf = weakSelf;
if ([strongSelf isExecuting]) {
[strongSelf cancel];
[strongSelf done]; // if canceling calls the delegate method that calls `done`, then you don't need this here
}
});
}
- (void)main
{
IDBAssert0(self.bestCapture.webpCandidate);
self.finished = NO;
self.executing = YES;
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
UIImage *possiblycorrupted = [UIImage imageWithWebPData:self.bestCapture.webpCandidate];
NSTimeInterval webpInterval = [NSDate timeIntervalSinceReferenceDate]-now;
NSDLog(@"it tooke %.2f sec to unpack webp", webpInterval);
self.microblinkCandidate = possiblycorrupted; // data superclass nsoperation processes
[super main];
}
基础 class 中的第一件事自然是将 finished 设置为 no 并将 execution 设置为 yes:
- (void)main
{
self.finished = NO;
self.executing = YES;
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
start = now;
CGSize size = [self.microblinkCandidate size];
IDBAssert0(size.width && size.height);
IDBAssert0(self.microblink);
// this starts async processing
[self.microblink processImage:self.microblinkCandidate
scanningRegion:CGRectMake(0.0, 0.0, 1.0, 1.0)
delegate:self];
while (![self isCancelled])
{
sleep(1);
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
if(now - start > 5) {
// #5677 microblink watchdog to detect hangs
[self cancel];
break;
}
}
[self done];
}
因为它不是摘要,也将单独使用。
循环仅用于 debug/watchdog 目的
在正常操作中它没有跳闸操作完成 如果这个回调:
- (void)scanningViewController: (UIViewController<PPScanningViewController>*)scanningViewController
didOutputResults:(NSArray*)results
{
if([results count]>0) {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
NSDLog(@"found barcode in %.1fs", now - start);
self.microblinkSuccessHandler();
}else{
IDBAssert0(self.microblinkFailureHandler);
self.microblinkFailureHandler();
}
[self done];
}
在 "processImage:" 完成时调用(及时)。
最基本的class是
@implementation IDBAsynchronousOperation
@synthesize executing = _executing;
@synthesize finished = _finished;
-(BOOL)isFinished
{
return _finished;
}
- (void)setFinished:(BOOL)finished
{
[self willChangeValueForKey:@"isFinished"];
_finished = finished;
[self didChangeValueForKey:@"isFinished"];
}
-(BOOL)isExecuting
{
return _executing;
}
- (void)setExecuting:(BOOL)executing
{
[self willChangeValueForKey:@"isExecuting"];
_executing = executing;
[self didChangeValueForKey:@"isExecuting"];
}
- (instancetype)init
{
self = [super init];
if (self) {
// self.completionBlock = ^{
// NSDLog(@"image barcode search has finished");
// };
IDBAssert0(sizeof(_executing)<2);
}
return self;
}
-(BOOL)isAsynchronous
{
return YES;
}
@end
您当然可以(我们经常这样做)subclass 您自己的具体 NSOperation
subclass。
要使基础 class 成为子class,您需要确保只执行一次 self.executing = true
。现在,base class 和 subclass 中的 main
都会执行此操作,因此您将执行两次。典型的解决方案是将它从这两个 main
实现中拉出来,并在基础 class 的 start
中进行。无论如何,Apple 建议您在 start
中执行此操作。
因此从两个 main
实现中删除了 self.finished
和 self.executing
内容,然后您可以实现 start
:
- (void)start {
if ([self isCancelled]) {
self.finished = YES;
return;
}
self.executing = YES;
[self main];
}
请注意,您不必在操作开始时调用 self.finished = false
,因为那样会发送不必要的 KVO。
一个不相关的观察:
如果您在基础 class 中保留 while
循环,如果 [self isCancelled]
或者 processImage
委托完成方法是调用(也许您可以更新一些状态 属性 以指定调用该委托方法的时间)。现在,如果 processImage
在超时之前完成,它将保持操作 运行 整整 5 秒。
就我个人而言,根据 processImage
的设计方式,我可能倾向于完全删除 while
循环。您通常希望完全避免任何这样的轮询。例如,我可能会将 [self done]
放在适当的委托方法中,然后设置一个计时器或 dispatch_after
超时。
- (void)main {
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate];
start = now;
CGSize size = [self.microblinkCandidate size];
IDBAssert0(size.width && size.height);
IDBAssert0(self.microblink);
// this starts async processing
[self.microblink processImage:self.microblinkCandidate
scanningRegion:CGRectMake(0.0, 0.0, 1.0, 1.0)
delegate:self];
// cancel upon timeout
typeof(self) __weak weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
typeof(self) __strong strongSelf = weakSelf;
if ([strongSelf isExecuting]) {
[strongSelf cancel];
[strongSelf done]; // if canceling calls the delegate method that calls `done`, then you don't need this here
}
});
}