我的应用程序必须在 iOS 3.2 上运行,并且 -addOperationWithBlock: 等方法仅适用于 > 4.0。
但是 NSOperationQueue 从 iOS 2.0 开始就可用了,所以我想尝试一下“旧方式”。有谁知道一个方便的教程,它显示了如何在没有块的情况下使用 NSOperationQueue 的基础知识?
我的应用程序必须在 iOS 3.2 上运行,并且 -addOperationWithBlock: 等方法仅适用于 > 4.0。
但是 NSOperationQueue 从 iOS 2.0 开始就可用了,所以我想尝试一下“旧方式”。有谁知道一个方便的教程,它显示了如何在没有块的情况下使用 NSOperationQueue 的基础知识?
调用操作非常简单。这些操作允许您使用某些对象参数(可选)向特定对象发送消息。
因此,给定您要调用的此方法:
- (void)doSomething {
NSLog (@"Did it!");
}
你可以做这样的事情来实现它:
// Get or create some queue
NSOperationQueue *someQueue = [NSOperationQueue mainQueue];
// create an invocation operation
NSInvocationOperation *invocationOp = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(doSomething)
object:nil];
[someQueue addOperation:invocationOp]; // Add the operation to the queue
[invocationOp release];
希望有帮助。
@Firoze Lafeer 给出了 NSInvocation 操作的示例,但您也可以使用自己的 NSOperation 子类。
官方文档通过示例显示了您可以使用的每种类型的操作。即使有可用的块,有时也更喜欢使用 NSOperation 子类来完成更大的任务。
在这里找到了一个很好的教程。它还超越了主题,并提供了有关为什么在主线程上获取数据并不总是一个好主意的信息。