gpt4 book ai didi

cocoa-touch - 为什么我的 NSOperation 子类永远不会完成?

转载 作者:行者123 更新时间:2023-12-04 02:34:08 25 4
gpt4 key购买 nike

我有一个 NSOperation我想同时运行的子类。

我的理解是,要使并发操作起作用:

  • 我需要定义 isConcurrent返回 YES .
  • 我需要定义 start方法
  • 我需要为 isExecuting 发送 KVO 通知和 isFinished完成后。
  • 使用 @synthesizeisExecuting 的值达到时,将自动发送适当的 KVO 通知和 isFinished被改变。

  • 尽管如此,我已经确认我的队列永远不会移动到下一个项目。

    这是我的代码的主要内容:
    @interface MyOperation()

    @property (readwrite) BOOL isExecuting;
    @property (readwrite) BOOL isFinished;

    @end

    @implementation MyOperation

    - (void)start
    {
    @autoreleasepool {
    self.isExecuting = YES;
    self.HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];

    _HTTPOperation.completionBlock = [^{
    [self completed];

    self.isExecuting = NO;
    self.isFinished = YES;
    } copy];

    [_HTTPOperation start];
    }
    }

    - (BOOL)isConcurrent
    {
    return YES;
    }

    - (void)completed
    {
    }

    @end

    我错过了什么?

    (这是在 iPhone 上,但我无法想象这很重要。)

    最佳答案

    看起来像任何 KVO 通知 @synthesize发送不够 NSOperationQueue继续前进。

    手动发送通知可以解决问题:

    - (void)start
    {
    @autoreleasepool {
    [self willChangeValueForKey:@"isExecuting"];
    self.isExecuting = YES;
    [self didChangeValueForKey:@"isExecuting"];

    NSURLRequest *URLRequest = [self buildRequest];
    if (!URLRequest) {
    [self willChangeValueForKey:@"isFinished"];
    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = NO;
    _isFinished = YES;
    [self didChangeValueForKey:@"isExecuting"];
    [self didChangeValueForKey:@"isFinished"];
    return;
    }

    self.HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];

    _HTTPOperation.completionBlock = [^{
    [self completed];

    [self willChangeValueForKey:@"isFinished"];
    [self willChangeValueForKey:@"isExecuting"];
    _isExecuting = NO;
    _isFinished = YES;
    [self didChangeValueForKey:@"isExecuting"];
    [self didChangeValueForKey:@"isFinished"];
    } copy];

    [_HTTPOperation start];
    }
    }

    也可以看看:
  • Why does NSOperation disable automatic key-value observing?
  • 关于cocoa-touch - 为什么我的 NSOperation 子类永远不会完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10422883/

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