NSOperation userInfo字典

Pet*_*isu 2 multithreading nsoperation nsoperationqueue ios

有没有办法如何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

}
Run Code Online (Sandbox Code Playgroud)

Dan*_*lly 6

NSOperation是指子类.只需设计自己的子类.

NSOperation类是一个抽象类,用于封装与单个任务关联的代码和数据.因为它是抽象的,所以不直接使用此类,而是使用系统定义的子类(NSInvocationOperation或NSBlockOperation)子类或使用其中一个来执行实际任务.

在这里阅读

我同意@ Daij-Djan关于userInfo财产增加的意见.
此属性可以作为扩展实现NSOperation(请参阅他的实现答案).
但是,对a的标识符的需求NSOperation是类的特化(你可以说新类是一个IdentifiableOperation)