我有一个需要相当长时间的任务,应该在后台运行。根据文档,这可以使用NSOperationQueue
. 但是,我不想保留 的类全局副本,NSOperationQueue
因为我真的只将它用于那一项任务。因此,我只是将它设置为自动释放,并希望它在任务完成之前不会被释放。有用。
像这样:
NSInvocationOperation *theTask = [NSInvocationOperation alloc];
theTask = [theTask initWithTarget:self
selector:@selector(doTask:)
object:nil];
NSOperationQueue *operationQueue = [[NSOperationQueue new] autorelease];
[operationQueue addOperation:theTask];
[theTask release];
不过,我有点担心。这能保证工作吗?或者可能operationQueue
会在某个时候被释放并接受theTask
它?