gpt4 book ai didi

objective-c - NSOperation - main 方法在主线程上运行?

转载 作者:行者123 更新时间:2023-11-28 20:41:49 25 4
gpt4 key购买 nike

为什么我的 NSOperation 子类在主线程上运行 main 方法?我希望它异步运行。

@interface ServiceClient : NSOperation
@end

@implementation ServiceClient

- (void)main
{
if ([[NSThread currentThread] isEqual:[NSThread mainThread]])
{
NSLog(@"Why does this get called on the main thread");
}
}

@end

开始操作

ServiceClient *serviceClient = [[ServiceClient aloc] init];
[serviceClient start];

编辑:

文档建议覆盖 isCuncurrent 并返回 yes,但不会调用该方法。

- (BOOL)isConcurrent
{
return YES;
}

最佳答案

需要在start方法中设置自己的线程

摘自 Concurrency Programming Guide 的片段

start
(Required) All concurrent operations must override this method and replace the default behavior with their own custom implementation. To execute an operation manually, you call its start method. Therefore, your implementation of this method is the starting point for your operation and is where you set up the thread or other execution environment in which to execute your task. Your implementation must not call super at any time.

如果您将 NSOperation 添加到 NSOperationQueue,它会为您处理这件事。

关于objective-c - NSOperation - main 方法在主线程上运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8232333/

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