Featured post
iphone - Cancel NSOperation in for loop? -
i trying implement search on background thread using nsoperation
on ios
. didn't want subclass nsoperation
i'm doing:
[searchqueue cancelalloperations]; nsinvocationoperation *op = [[nsinvocationoperation alloc] initwithtarget:self elector:@selector(filtercontentforsearchtext:) object:self.searchdisplaycontroller.searchbar.text]; [searchqueue addoperation:op]; [op release];
the search method includes loop checks whether being searched in array. when cancel nsoperation
calling cancelalloperations
, loop continues run through array. prevent , wondering whether legit call within loop:
if ([[[searchqueue operations] objectatindex:0] iscancelled]) { [tmp_array release]; // tmp_array used hold temporary results [pool drain]; // pool autorelease pool return; }
one of reasons subclass nsoperation
implement proper cancellation. approach, violates several design principles. basically, since cancellation requires cooperation of operation itself, nsinvocationoperation
isn't built cancel invocation while it's executing (though can cancelled before starts executing), running method shouldn't know how it's called.
instead, if subclass nsoperation
, can put of functionality main
method easily:
@implementation myoperation - (void)main { if ([self iscancelled]) return; (...) { // stuff if ([self iscancelled]) { [tmp_array release]; return; } } } @end
note don't have maintain own autorelease pool such implementation.
- Get link
- X
- Other Apps
Comments
Post a Comment