我的应用程序必须在iOS 3.2上运行,并且像-addOperationWithBlock: only in > 4.0这样的方法可以工作。
但是从iOS 2.0开始,NSOperationQueue就可以使用了,所以我想尝试一下“老方法”。有没有人知道一个很方便的教程,它展示了如何在没有块的情况下使用NSOperationQueue的基础知识?
发布于 2011-05-31 23:06:44
使用调用操作非常简单。这些操作允许您使用某个对象参数(可选)向特定对象发送消息。
因此,给定您想要调用的这个方法:
- (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];希望这能有所帮助。
发布于 2011-05-31 23:22:08
@Firoze Lafeer给出了一个NSInvocation操作的例子,但你也可以使用你自己的NSOperation子类。
official documentation通过示例显示了您可以使用的每种类型的操作。即使有块可用,有时还是更喜欢使用NSOperation子类来处理更大的任务。
发布于 2011-08-15 18:54:24
我找到了一个很好的教程here。它还超越了主题,并提供了为什么在主线程上获取数据并不总是一个好主意的信息。
https://stackoverflow.com/questions/6188866
复制相似问题