gpt4 book ai didi

ios - daisy NSOperation main 变成 super 是否可以接受?

转载 作者:行者123 更新时间:2023-11-29 11:56:29 24 4
gpt4 key购买 nike

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

基类中 main 自然要做的第一件事就是将 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];
}

因为它不是摘要,也将单独使用。

循环仅用于调试/看门狗目的

在正常操作中,它不会跳闸,操作已完成如果这个回调:

- (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:”完成时调用(及时)。

基类是

@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

最佳答案

您当然可以(我们经常这样做)子类化您自己的具体 NSOperation 子类。

要使基类可继承,您需要确保只执行一次 self.executing = true。现在,基类和子类中的 main 都会执行此操作,因此您将执行两次。典型的解决方案是将它从这两个 main 实现中拉出来,并在基类的 start 中完成。无论如何,Apple 建议您在 start 中执行此操作。

因此从两个 main 实现中删除了 self.finishedself.executing 东西,然后你可以实现 start:

- (void)start {
if ([self isCancelled]) {
self.finished = YES;
return;
}

self.executing = YES;

[self main];
}

请注意,您不必在操作开始时调用 self.finished = false,因为那样会发送不必要的 KVO。


一个不相关的观察:

如果您在基类中保留 while 循环,我建议在 [self isCancelled]processImage delegate completion methods was called(也许你可以更新一些状态属性来指定调用该委托(delegate)方法的时间)。现在,如果 processImage 在超时前完成,它将保持操作运行整整 5 秒。

就个人而言,根据 processImage 的设计方式,我可能倾向于完全去除 while 循环。您通常希望完全避免任何这样的轮询。例如,我可能会将 [self done] 放在适当的委托(delegate)方法中,然后设置一个计时器或 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
}
});
}

关于ios - daisy NSOperation main 变成 super 是否可以接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com