Pad*_*215 7 objective-c nsoperationqueue ios
我需要做一系列的url调用(获取WMS tile).我想使用LIFO堆栈,因此最新的url调用是最重要的.我想现在在屏幕上显示瓷砖,而不是5秒前在屏幕上显示在屏幕上的瓷砖.
我可以从NSMutableArray创建自己的堆栈,但我想知道NSOperationQueue是否可以用作LIFO堆栈?
您可以使用 设置操作队列中操作的优先级-[NSOperation setQueuePriority:]。每次添加操作时,您都必须重新调整现有操作的优先级,但您可以实现与您正在寻找的类似的东西。你基本上会降级所有旧的,并给最新的一个最高优先级。
遗憾的是,我认为NSOperationQueues ,顾名思义,只能用作队列 \xe2\x80\x94 ,不能用作堆栈。为了避免必须执行大量手动编组任务,最简单的方法可能是将队列视为不可变的并通过复制进行变异。例如
- (NSOperationQueue *)addOperation:(NSOperation *)operation toHeadOfQueue:(NSOperationQueue *)queue\n{\n // suspending a queue prevents it from issuing new operations; it doesn't\n // pause any already ongoing operations. So we do this to prevent a race\n // condition as we copy operations from the queue\n queue.suspended = YES;\n\n // create a new queue\n NSOperationQueue *mutatedQueue = [[NSOperationQueue alloc] init];\n\n // add the new operation at the head\n [mutatedQueue addOperation:operation];\n\n // copy in all the preexisting operations that haven't yet started\n for(NSOperation *operation in [queue operations])\n {\n if(!operation.isExecuting)\n [mutatedQueue addOperation:operation];\n }\n\n // the caller should now ensure the original queue is disposed of...\n}\n\n/* ... elsewhere ... */\n\nNSOperationQueue *newQueue = [self addOperation:newOperation toHeadOfQueue:operationQueue];\n[operationQueue release];\noperationQueue = newQueue;\nRun Code Online (Sandbox Code Playgroud)\n\n目前看来,释放仍在工作的队列(就像旧操作队列一样)不会导致它取消所有操作,但这不是记录的行为,因此可能不值得信赖。如果你想真正安全,键值观察operationCount旧队列上的属性,并在它变为零时释放它。
| 归档时间: |
|
| 查看次数: |
4706 次 |
| 最近记录: |