有没有办法让 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.
我同意@Daij-Djan 关于添加userInfo
属性的观点。
此属性可以作为 NSOperation
的扩展来实现(请参阅他的实现答案)。
但是,NSOperation
需要标识符是类的特化(您可以说新类是 IdentifiableOperation
)
关于ios - NSOperation 用户信息字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16251257/