gpt4 book ai didi

objective-c - 复合 NSOperation。这是一个坏主意吗?

转载 作者:搜寻专家 更新时间:2023-10-30 20:23:06 25 4
gpt4 key购买 nike

对于我正在处理的 iOS4.X 应用程序,我们经常需要执行 HTTP 请求,然后解析结果,并对结果执行某些操作,等等。

为此,我创建了一个 NSOperation 类,以允许使用 NSOperation 队列组合 NSOperation。将 NSOperationQueues 用于此类小事情是否存在任何问题。有些人告诉我队列应该是一个更永久的东西。

我不希望我们的应用程序中的嵌套深度超过 2 层。

下面是这种用法的一个例子:

@implementation CompositeOperation

- (id)initWithOperations:(NSArray *)operations {
if ((self = [super init])) {
operations_ = [operations retain];
[[operations_ lastObject] addObserver:self forKeyPath:@"isFinished" options:NSKeyValueObservingOptionNew context:nil];

}
return self;
}

-(void)dealloc {
[operations_ release];
[operationQueue_ release];
[super dealloc];
}

- (BOOL)isConcurrent {
return YES;
}

@synthesize isExecuting = isExecuting_;
@synthesize isFinished = isFinished_;
@synthesize operations = operations_;

- (void) start {
if (![self isCancelled]) {
operationQueue_ = [[NSOperationQueue alloc] init];
// TODO: Add code to execute this serially
[operationQueue_ addOperations:operations_ waitUntilFinished:NO];
}
}

- (void)cancel {
if (operationQueue_) {
[operationQueue_ cancelAllOperations];
}
[super cancel];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"isFinished"] && object == [operations_ lastObject]) {
[self setIsFinished:YES];
}
}

@end

谢谢,迈克

最佳答案

我认为这是一个非常好的主意,所以我什至在它之后创建了库:CompositeOperations .

有两种操作:COSimpleOperation表示的简单操作COCompositeOperation 表示的对象和复合操作对象。

简单的操作是尽可能小的单元——引用文档:

In a nutshell COSimpleOperation is a NSOperation with a small bit of convenience sugar on top of it. As an operational unit for composite operations it usually corresponds to one networking request or some small focused piece of work.

复合操作是由子操作组成的操作。引用@mikelikespie:

The point of this object is to make it so one can represent multiple operations that are logically grouped as one operation.

...这几乎是四种设计模式组合中对复合设计模式的另一种描述。

复合操作可以parallelsequential .

并行操作的创建与所讨论的代码示例相同:

NSArray *operations = @[ 
operation1, operation2, operation3
]; // each operation is NSOperation <COOperation> *

COCompositeOperation *parallelOperation = [[COCompositeOperation alloc] initWithOperations:operations];

要创建顺序操作,应使用符合 COSequence 的对象实例化 COCompositeOperation协议(protocol):

Sequential composition implies sequential flow: sub-operations are executed serially one after another. Sequencing is achieved via collaboration between COCompositeOperation and arbitrary class conforming to COSequence protocol which is used by composite operation as a delegate who decides what operations are and in which order to run them.

为了使这种操作组合成为可能,我需要对操作库的工作进行一些限制:除了是 NSOperations 之外,COSimpleOperation 和 COCompositeOperation 还符合 <COOperation>协议(protocol):

This conformance basically means that both operations when finished have 3 possible states:

  1. a non-empty result field indicates success

  2. a non-empty error field indicates failure

  3. both empty result and error fields indicate that operation was cancelled from outside (using -[NSOperation cancel] method).

Operation can never have both result and error fields non-empty!

This convention allows Composite Operations to decide at a certain point whether to continue execution of particular group of operations or to stop it. For operations without a specific result [NSNull null] should be passed as result.


对我来说,这个库背后的理性“只是”能够表示操作,以便“它们在逻辑上被分组为一个操作”。有些库实现了相同类型的高级功能,但同时它们引入了如下概念:ReactiveCocoa 中的信号|或 PromiseKit 中的 promise 我真的不需要,或者我会说不同意。我想要一些尽可能简单的东西,并且基于良好的、众所周知的 NSOperation/NSOperationQueue 基础设施,所以这就是整个努力的重点。

附言我希望这种答案适合 SO,至少它与 @mikelikespie 大约 4 年前的提问完全一致。

关于objective-c - 复合 NSOperation。这是一个坏主意吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4743953/

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