2011-09-20 29 views
2

我有几个后台任务要处理,同时仍然保持UI响应。我开始创建和管理线程的路径,但很快就遇到了NSOperations。听起来像是更好的解决方案。 。 。iphone,如何创建NSOperationQueue

但是,我似乎无法获得对NSOperationQueue的参考。线程编程指南建议如下:

@implementation MyCustomClass 
- (void)launchTaskWithData:(id)data 
{ 

    NSInvocationOperation* theOp = [[NSInvocationOperation alloc] initWithTarget:self 
        selector:@selector(myTaskMethod:) object:data]; 

    // Add the operation to the internal operation queue managed by the application delegate. 
    [[MyAppDelegate sharedOperationQueue] addOperation:theOp]; 
} 

// This is the method that does the actual work of the task. 
- (void)myTaskMethod:(id)data 
{ 
    // Perform the task. 
} 

。 。 。但我(更重要的是,编译器)在我的应用程序中实现代码时看不到'sharedOperationQueue'消息。

我在想什么?是否sharedOperationQueue已弃用且不再可用?我怎样才能得到一个NSOperationQueue引用?

回答

2

sharedOperationQueue不是官方API的一部分。这是一个自定义的方法,你应该自己实现,在这个例子中作为你的应用程序委托类中的类方法。该方法应该创建并返回NSOperationQueue,或者如果它已经创建了队列,则只需返回现有的队列。

你如何在你的情况下实现这一点取决于你。你不必效仿这个例子。使用alloc/init(在这里没有魔法)简单地创建一个操作队列,并将其引用存储在属性中,以便稍后可以在不再需要时释放它。

1

可可是我的女朋友有一个good tutorial,这将帮助您使用NSOperationQueue。

+0

链接,请123 – atreat

-1

您可以选择添加操作之前声明应用程序委托:

AppDelegateClass * MyAppDelegate = [NSApp delegate];