I am creating a task (NSOperation) and putting it in an NSOperationQueue
TaskJsonParser *parseTask = [[TaskJsonParser alloc] initWithJsonString:responseString andDelegate:self];
[self.opQueue addOperation:parseTask];
When the task completes it calls a delegate method and sets it's flags to "finished". But Instruments shows it's still in memory... And indicates the above code as it's creator.
Why doesn't the queue release it after the operation completes. There is not other strong reference to it...
This is the code of the NSOperation
@interface TaskJsonParser ()
@property (strong, nonatomic) NSString *stringToParse;
@property (strong, nonatomic) SBJson4Parser *jsonParser;
@end
@implementation TaskJsonParser
#pragma mark - Custom initialization
- (instancetype)initWithJsonString:(NSString *)jsonString andDelegate:(id<JsonParsingDelegate>) delegate
{
self = [super init];
if (self) {
_delegate = delegate;
_stringToParse = jsonString;
self.TAG = @"TaskJsonParse";
// configure the parser
SBJson4ValueBlock block = ^(id v, BOOL *stop) {
BOOL isDictionary = [v isKindOfClass:[NSDictionary class]];
if (isDictionary) {
NSDictionary *parseResult = (NSDictionary *)v;
// [self.delegate didFinishParsingJsonWithResult:parseResult];
[(NSObject *)self.delegate performSelector:@selector(didFinishParsingJsonWithResult:) withObject:parseResult];
} else {
NSLog(@"json parsing did not result in a NSDictionary, returnig %@",NSStringFromClass([v class]));
[(NSObject *)self.delegate performSelector:@selector(didFinishParsingJsonWithResult:) withObject:v];
// [self.delegate didFinishParsingJsonWithResult:v];
}
};
SBJson4ErrorBlock errorBlock = ^(NSError* err) {
NSLog(@"OOPS parsing error: %@", err);
MvpError *e = [[MvpError alloc] initWithErrorObject:err andType:Parser];
// [self.delegate didFailToParseJsonWithError:e];
[(NSObject *)self.delegate performSelector:@selector(didFinishParsingJsonWithResult:) withObject:e];
};
_jsonParser = [SBJson4Parser multiRootParserWithBlock:block
errorHandler:errorBlock];
if (debugLog) { NSLog(@"Allocating Task: %@;",self.TAG); }
}
return self;
}
#pragma mark - Overriden NSObject methods
// requierd for concurrent running
- (void)main
{
@autoreleasepool {
if (self.isCancelled) {
[self completeOperation];
return;
}
SBJson4ParserStatus responseCode = [self.jsonParser parse:[self.stringToParse dataUsingEncoding:NSUTF8StringEncoding]];
if (responseCode == SBJson4ParserComplete) {
NSLog(@"parse complete");
} else if (responseCode == SBJson4ParserError) {
NSLog(@"failed to parse");
}
[self completeOperation];
}
}
Could thous block in the init... method cause the problem?