ios - NSOperation 用户信息字典

标签 ios multithreading nsoperation nsoperationqueue

有没有办法让 userInfo NSDictionary 用于 NSOperation

基本上我想为一个 NSOperation 分配一个 ID,稍后我想检查这个 ID 是否已经分配给一个 NSOperation

- (void)processSmthForID:(NSString *)someID {

    for (NSOperation * operation in self.precessQueue.operations) {

        if ([operation.userInfo[@"id"] isEqualToString:someID]) {
            // already doing this for this ID, no need to create another operation
            return;
        }

    }

    NSOperation * newOperation = ...
    newOperation.userInfo[@"id"] = someID;

    // enqueue and execute

}

最佳答案

NSOperation 意味着被子类化。只需设计您自己的子类。

The NSOperation class is an abstract class you use to encapsulate the code and data associated with a single task. Because it is abstract, you do not use this class directly but instead subclass or use one of the system-defined subclasses (NSInvocationOperation or NSBlockOperation) to perform the actual task.

read here

我同意@Daij-Djan 关于添加userInfo 属性的观点。
此属性可以作为 NSOperation 的扩展来实现(请参阅他的实现答案)。
但是,NSOperation 需要标识符是类的特化(您可以说新类是 IdentifiableOperation)

关于ios - NSOperation 用户信息字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251257/

相关文章:

ios - UITableViewCell 上 2 个 UILabel 的布局

java - Thread 中的 repaint() 导致 CPU 使用率很高

java - 在不更改代码的情况下中断在 java 进程中运行的挂起线程的命令

python - 何时在 Python 中使用线程本地内存?

ios - iTunes App Store错误“此版本无效。”

ios - 如何实现巨型纹理

iphone - initWithContentsOfURL会在NSOperation子类中泄漏内存。还有其他人看到吗?

ios - 快速缓存.. iOS FBApi

ios - 实现可以在 Bolts Framework 中取消的任务 (BFTask)

ios - Swift:使用 collectionView 创建 imageView 堆栈