0

I have class MyOperation : NSOperation with @property (nonatomic, retain) NSString *oID;

And sometimes I need to cancel operation with specific oID. I'm trying to do this:

NSArray *operations = operationQueue.operations;
NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat: @"oID == %@", _specificID]];
NSArray *arrayOperations = [operations filteredArrayUsingPredicate: predicate];

and get error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "oID == 0f5db97b-f127-4425-ad79-451d1f204016"'

Is it possible to filter operation from NSOperationQueue?

Rost K.
  • 262
  • 2
  • 14

2 Answers2

0

I'm going to guess that the operations.queue is a mutable array (logging its class returns this '__NSArrayM'). What you should try is:

NSArray *operations = [NSArray arrayWithArray:operationQueue.operations];

That said, some operation may have completed (and been removed from the mutable queue) by the time the predicate is applied to it.

David H
  • 40,852
  • 12
  • 92
  • 138
0

Your problem is that you are formatting a string, then sending that to the Predicate constructor method. It's already a formatter, and knows how to format the data you give it.

Basically, you need the format to look like:

oID == "0f5db97b-f127-4425-ad79-451d1f204016"

but you are getting

oID == 0f5db97b-f127-4425-ad79-451d1f204016

If you use the formatter by itself, you should get past this issue...

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"oID == %@", _specificID];

NOTE: The predicate formatter knows it should handle strings specially, and automatically adds the extra quotation characters when you pass a string to be formatted by %@'

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87